【问题标题】:Recyclerview Horizontal scroll,How to display one item at a scroll like pagerRecyclerview水平滚动,如何像寻呼机一样在滚动中显示一个项目
【发布时间】:2017-03-26 03:55:51
【问题描述】:

我必须使用 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>

【问题讨论】:

  • 那么使用ViewPager有什么问题呢?
  • @Wizard 没问题,但我在 Sigle List 中处理这个问题。所以如果是RecyclerView就好了。因为我已经用 Vertical View 实现了 recyclerView 中的所有功能。
  • 这可能不保证一个完整的答案,所以只是给你和任何有类似问题的人的建议:另一个选择是简单地使用 ViewPager2,它基于 RecyclerView 但也有 ViewPager能力。

标签: android android-viewpager android-recyclerview


【解决方案1】:

如果你想让 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);

【讨论】:

  • 如何为垂直滚动回收器视图归档相同的内容(一次只显示一行)?
  • @kavie 将 LinearLayoutManager.HORIZONTAL 更改为 LinearLayoutManager.VERTICAL
  • 如何做到这一点,但屏幕上只有 n 个数字?
猜你喜欢
  • 2016-02-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多