You need to extend the FragmentPagerAdapter or FragmentStatePagerAdapter to easily insert a RecyclerView. If you intend to update the contents of a ViewPager during its life cycle, it is strongly recommended that you use the FragmentStatePagerAdapter
You will need to create an additional fragment layout containing a RecyclerView .
If you want to update the ViewPager using SwipeRefreshLayout , do not terminate it using SwipeRefreshLayout . Instead, you should have a SwipeRefreshLayout inside the fragment layout.
So for your snippet you can get the following xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listRefresh" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/categoryList" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout>
And create an additional Fragment class that will inflate this layout and implement the methods that you will need to update the status of the update indicator.
Here's a slightly old example: http://developer.android.com/training/implementing-navigation/lateral.html
If you want to connect ViewPager to the new TabLayout support library, this is easy to do with:
tabLayout.setupWithViewPager(viewPager);
Finally, if you update your “fragmented” ViewPager , do not try to adapt to the reset adapter, since fragments are not managed with the adapter, but with the FragmentManager . Reasonably update the contents of the corresponding RecyclerViews
public class MyFragmentedPagerAdapter extends FragmentStatePagerAdapter { private final TabLayout mTabLayout; private final SwipeRefreshLayout.OnRefreshListener mRefreshListener; private Vector<PriceListFragment> fragmentList; private Vector<String> titles; public MyFragmentedPagerAdapter(FragmentManager fm, MyComplexData data, OnCartActionListener listener, TabLayout tabLayout, SwipeRefreshLayout.OnRefreshListener refreshListener) { super(fm); mTabLayout = tabLayout; // external refresh listener, that will trigger an updateData() mRefreshListener = refreshListener; fragmentList = new Vector<>(); titles = new Vector<>(); updateData(data); } public void updateData(MyComplexData data) { boolean updateTabs = false; boolean hasNewData = false; Vector<String> newTitles = new Vector<>(); int position = 0; for(TabContents tabContents : data.getTabContents()) { if(tabContents.getListContents() == null) continue; hasNewData = true; boolean isNewFragment; MyFragment fragment; try { fragment = fragmentList.get(position); isNewFragment = false; } catch (ArrayIndexOutOfBoundsException e) { fragment = new MyFragment(); isNewFragment = true; } // Update the data, title and hide update indicator of SwipeRefreshLayout fragment.setTabContents(tabContents); newTitles.add(tabContents.getName()); if(isNewFragment) { fragment.setRefreshListener(mRefreshListener); fragmentList.add(fragment); } position++; } if(!hasNewData) return; // we need to decide, whether to update tabs if(titles.size() != newTitles.size()) { updateTabs = true; } else { for(position = 0; position < titles.size(); position++) { if(!titles.get(position).equals(newTitles.get(position))) { updateTabs = true; break; } } } titles = newTitles; notifyDataSetChanged(); if(updateTabs) mTabLayout.setTabsFromPagerAdapter(this); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } // You need to override this method as well @Override public int getItemPosition(Object object) { MyFragment fragment = (MyFragment) object; String title = (String) fragment.getTitle(); int position = titles.indexOf(title); if (position >= 0) { return position; } else { return POSITION_NONE; } } @Override public int getCount() { return titles.size(); } @Override public CharSequence getPageTitle(int position) { return fragmentList.get(position).getTitle(); } }
Your MyFragment class must implement the getTitle() method.