Recyclerviews in ViewPager

后端 未结 1 1935
一个人的身影
一个人的身影 2020-12-15 08:59

I\'m trying to get this working: I basically want two Recyclerviews in one ViewPager. I followed this Tutorial: http://android-java-development.blogspot.de/2012/05/system.ht

相关标签:
1条回答
  • 2020-12-15 09:33

    You have to extend FragmentPagerAdapter or FragmentStatePagerAdapter in order to easily embed RecyclerView. If you are going to update your ViewPager contents during its lifecycle it is strictly recommended to use FragmentStatePagerAdapter

    You will have to create additional fragment layout, containing RecyclerView.

    If you wish to update your ViewPager with SwipeRefreshLayout, don't wrap it with SwipeRefreshLayout. Instead, you must have SwipeRefreshLayout inside fragment layout.

    Therefore for your fragment you may 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 additional Fragment class, which will inflate that layout and implement methods, that you will need to update refresh indicator status.

    A bit old example is found here: http://developer.android.com/training/implementing-navigation/lateral.html

    If you wish to connect your ViewPager with new support library TabLayout, it is easily one with:

    tabLayout.setupWithViewPager(viewPager);
    

    Finally, if you will update your "fragmented" ViewPager, don't try to reset the adapter, as fragments are managed not with adapter, but with FragmentManager. It is wiser to update content of 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 has to implement getTitle() method.

    0 讨论(0)
提交回复
热议问题