0

I'm currently implementing swipeRefreshLayout the problem was, it's still not showing up. I tried to implement some of the answer in this link ,and this link ,and this link .

my gradle module: compile 'com.android.support:appcompat-v7:23.1.1'

Here's my sample xml:recycler_view

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_relative_root"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/feeds_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.v4.widget.SwipeRefreshLayout>

Java Code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //INFLATE
        mRelativeLayoutRoot = (RelativeLayout) inflater.inflate(R.layout.recycler_view,container,false)
                                                       .findViewById(R.id.recycler_relative_root);
    //MAP SWIPE REFRESH
        mSwipeRefreshLayout = (SwipeRefreshLayout) mRelativeLayoutRoot.findViewById(R.id.feeds_swipe_refresh);

    //MAP RECYCLER VIEW
        recyclerView = (RecyclerView) mRelativeLayoutRoot.findViewById(R.id.my_recycler_view);

    //RECYCLER VIEW LAYOUT
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

    //ORG ADAPTER
        orgListAdapter = new OrgListAdapter(organizationDataList);
        recyclerView.setAdapter(orgListAdapter);

    //GET DATA REQUEST
        parseJSONData();

    return recyclerView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            parseJSONData();
        }
    });

    mSwipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });

    mSwipeRefreshLayout.setColorSchemeColors(R.color.appIntroColorFifth,
            R.color.appIntroColorFourth,
            R.color.appIntroColorOne,
            R.color.appIntroColorTwo);
}

Whenever I swipe down, nothing happens, swipe refresh doesn't show up either. I test this on an actual device running API 19 (Kitkat) and API 23 (Marshmallow) in emulator.

Community
  • 1
  • 1
RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56

1 Answers1

0

You're returning your RecyclerView in onCreateView(), not the entire view, so your fragment is only displaying your RecyclerView. You shouldn't need to get your root layout at all, just create the view and use it to find the subviews. And make sure you return the root view:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.recycler_view, container, false);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
    mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.feeds_swipe_refresh);

    // Your other create view code here...

    return view;
}

And if you really need the root RelativeLayout you can just call:

mRelativeLayout = (RelativeLayout) view;
Bryan
  • 14,756
  • 10
  • 70
  • 125
  • Thank you for reminding me. I miss that one. But maybe it will work if I `return mRelativeLayoutRoot;` I forgot that it always `return recyclerView;` – RoCkDevstack Jun 09 '16 at 13:34
  • How do I dismiss this thing if it already loaded my data in JSON? – RoCkDevstack Jun 09 '16 at 13:37
  • @RoCk Yes, it should work if you return `mRelativeLayoutRoot` as well. To dismiss the `SwipeRefreshLayout` you should have some sort of callback when you load your JSON data, in that callback you can call `mSwipeRefreshLayout.setRefreshing(false);`. – Bryan Jun 09 '16 at 13:42
  • Thanks bryan, working on it. It always shows up due to that runnable code. – RoCkDevstack Jun 09 '16 at 13:50
  • 1
    @RoCk Oh, right, I forgot to mention that. You should not need the `Runnable` at all, `mSwipeRefreshLayout.setRefreshing(true)` can run on the main thread. I would also suggest putting `setRefreshing(true)` at the top of your `parseJSONData()` method, this way it will start refreshing every time your reload your data. And then you can dismiss it in your callback, like I stated before. – Bryan Jun 09 '16 at 13:53
  • Is there a way I can slow down this thing? It loads too fast. I can only see it if I pull down. – RoCkDevstack Jun 09 '16 at 13:53
  • @RoCk You mean your data loads too fast? If that is the case, I would have to highly advise against slowing it down. If your data is no longer loading, there is no reason to continue to display a loading prompt. – Bryan Jun 09 '16 at 13:55
  • I have that in mind. Maybe because I'm doing this on an emulator. Not on actual device. Many Thank you for your help and time. – RoCkDevstack Jun 09 '16 at 13:57