ViewPager滑动不适用于RecyclerView

时间:2015-08-03 08:59:29

标签: android android-layout android-fragments android-viewpager android-recyclerview

我想使用ViewPager实现一个简单的滑动导航,其中包含用于保存和管理RecyclerView的片段。

片段已创建并可使用setCurrentItem()切换,但您无法向左或向右滑动以切换页面。 ViewPager似乎忽略了任何滑动手势。

我的活动布局:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"/>

    <android.support.v7.widget.Toolbar
                  ...                 />

</FrameLayout>

我使用FragmentPagerAdapter中的onCreate()填充ViewPager,如下所示:

viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.addOnPageChangeListener(this);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
       @Override
       public Fragment getItem(int position) {
           switch (position) {
               case 0:
                   return new Dummy2Fragment();

               case 1:
                   return new Dummy2Fragment();

               default:
                   return null;
           }
        }

       @Override
       public int getCount() {
           return 2;
       }

       @Override
       public CharSequence getPageTitle(int position) {
           return "Dataset " + (position + 1);
       }
   });

片段布局是一个简单的FrameLayout,它包装了一个RecyclerView:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_material_light"/>

</FrameLayout>

适配器和布局管理器设置在片段的onCreateView

recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 6, LinearLayoutManager.VERTICAL, false));

编辑#1

我想我的问题不够清楚 我有一个工作的Pager,其中有一个工作的TabLayout,它由一个'FragmentPagerAdapter'填充,这意味着正确创建了Fragments,并且标签布局显示了每个项目的可点击标签。此ViewPager中的每个片段都会显示RecyclerView GridLayoutManager

但是,ViewPager似乎没有收到任何滑动手势,您可以通过TabLayout更改当前所选位置,但不能通过从左到右滑动来更改。
看起来RecyclerView会消耗所有滑动事件,无论布局方向如何设置为vertical

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您可以像使用Viewpager一样使用tablayout

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


 </android.support.design.widget.CoordinatorLayout>

同时设置

   tabLayout = (TabLayout) rv.findViewById(R.id.tabs);
   tabLayout.setupWithViewPager(viewPager);

和方法setupWithViewPager就像

  private void setupViewPager(ViewPager viewPager) {
      adapter = new Adapter(getChildFragmentManager());
      adapter.addFragment(new MatesListFragment(), "Mates");
      adapter.addFragment(new FavoursListFragment(), "Favours");
      adapter.addFragment(new FavemeListFragment(), "FavMe");
        viewPager.setAdapter(adapter);
}


 class Adapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments = new ArrayList();
    private final List<String> mFragmentTitles = new ArrayList();

    public Adapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
        mFragmentTitles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

     public int getItemPosition(Object object) {
         return POSITION_NONE;
     }
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitles.get(position);
    }
}

在MatesListFragment,FavoursListFragment等中,您拥有RecyclerView布局