您必须扩展
FragmentPagerAdapter或
FragmentStatePagerAdapter为了轻松嵌入RecyclerView。如果要在其生命周期内更新ViewPager内容,则强烈建议使用
FragmentStatePagerAdapter
RecyclerView。
如果您希望更新您
ViewPager与
SwipeRefreshLayout,不与包装它
SwipeRefreshLayout。相反,您必须具有
SwipeRefreshLayout内部片段布局。
因此,对于您的片段,您可能会获得以下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>
并创建其他Fragment类,该类将扩充该布局并实现方法,您将需要更新刷新指示器状态。
在这里可以找到一个较旧的示例:http : //developer.android.com/training/implementing-
navigation/lateral.html
如果您想将ViewPager与新的支持库TabLayout连接起来,则很容易做到这一点:
tabLayout.setupWithViewPager(viewPager);
最后,如果您要更新“碎片化的”
ViewPager,请不要尝试重置适配器,因为片段不是通过适配器而是通过进行管理
FragmentManager。更新相应内容比较明智
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(); }}
您的
MyFragment课程必须实现
getTitle()方法。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)