ViewPager与RecyclerView滑动冲突,无法滑动。

10

我想使用ViewPager和包含和管理RecyclerView的片段来实现简单的滑动导航。

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

我的Activity布局:

<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>

我在我的onCreate()中使用FragmentPagerAdapter来填充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”填充,这意味着 Fragment 被正确创建,而选项卡布局显示了每个项目的可点击选项卡。此 ViewPager 中的每个 Fragment 都显示了一个使用 GridLayoutManager 的 RecyclerView。

然而,ViewPager 似乎没有接收到任何滑动手势,您可以通过 TabLayout 更改当前选择的位置,但例如从左向右滑动则不行。
看起来 RecyclerView 占用了所有滑动事件,无论布局方向如何,这是设置为“vertical”的。

有什么想法吗?


https://androidbeasts.wordpress.com/2015/08/11/tabs-with-swipe-views/#more-79 - Aakash
1个回答

2
你可以像这样使用tablayout和Viewpager:
<?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布局。

4
谢谢您的回复。我认为您误解了我的问题,我想知道为什么ViewPagers的滑动功能无法使用。我还有一个包含选项卡布局的版本活动,但那里也无法使用滑动手势。 - tobs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接