Recyclerview水平滚动,如何像Pager一样显示一个项目

28

我需要使用RecyclerView同时实现水平和垂直滚动,我可以通过使用LinearLayoutManager来设置方向以在代码中更改RecyclerView的方向。问题是当进行水平滚动时,它会在同一页上显示下一个项目。当我们滚动时应该只显示一个项目,请帮我修复这个问题或提供任何建议。

**main.xml**

<LinearLayout
    android:id="@+id/recyler_container"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp">
<android.support.v7.widget.RecyclerView
    android:id="@+id/vertical_recycler_view"
    android:layout_below="@id/slelect_scroll"
    android:background="#fff"
    android:layout_width="match_parent"
android:layout_height="wrap_content"/>

**row.xml**

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal"
     android:layout_width="match_parent"
     android:background="#3e56ed"
     android:layout_height="wrap_content">


<TextView
    android:textColor="#FFF"
    android:textSize="18sp"
    android:padding="16dp"
    android:id="@+id/txtView"
    android:text="sample text"
    android:layout_weight="1"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/txtView2"
    android:textColor="#FFF"
    android:textSize="18sp"
    android:padding="16dp"
    android:layout_marginLeft="20dp"
    android:layout_weight=".1"
    android:background="#000"
    android:layout_alignParentRight="true"
    android:text="sample text234"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

Verical SCroll

Horizontal scroll


那么使用 ViewPager 的问题是什么? - user2756345
@Wizard 没问题,但我正在使用单个列表处理此问题。如果可以使用RecyclerView会更好。因为我已经在RecyclerView中使用了垂直视图实现了所有功能。 - ASKAR ALI
这可能不需要完整的答案,所以只是作为给你和任何遇到类似问题的人的建议:另一个选择是简单地使用ViewPager2,它基于RecyclerView但也具有ViewPager的功能。 - Benjamin Basmaci
1个回答

94

如果您希望RecyclerView模仿ViewPager的行为——

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
SnapHelper snapHelper = new PagerSnapHelper();
recyclerView.setLayoutManager(layoutManager);
snapHelper.attachToRecyclerView(mRecyclerView);

如何为垂直滚动的 Recycler View 存档(每次只显示一行)? - kavie
@kavie 将 LinearLayoutManager.HORIZONTAL 更改为 LinearLayoutManager.VERTICAL - Misagh
2
如何在屏幕上仅显示n个数字来实现这个功能? - Tefa

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