我想使用带有保存和管理 RecyclerView 的 fragment 的 ViewPager 实现简单的滑动导航。
Fragment 已创建,可以使用 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);
}
});
fragment 布局是一个简单的 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>
适配器和布局管理器在 fragment 的onCreateView
中设置:
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 6, LinearLayoutManager.VERTICAL, false));
编辑 #1
我想我的问题问得不够清楚。
我有一个工作寻呼机和一个由“FragmentPagerAdapter”填充的工作 TabLayout,这意味着 fragment 已正确创建并且选项卡布局为每个项目显示一个可单击的选项卡。此 ViewPager 中的每个 fragment 都显示一个带有 GridLayoutManager
的 RecyclerView
。
但是,ViewPager 似乎没有收到任何滑动手势,您可以通过 TabLayout
更改当前选定的位置,但不能通过从左向右滑动等方式更改。
看起来 RecyclerView
会消耗所有滑动事件,而不管布局方向如何(设置为 vertical
)。
有什么想法吗?
最佳答案
您可以像这样将 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 布局
关于android - ViewPager 滑动不适用于 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783280/