【问题标题】:Dispatch horizontal RecycleView swipe events to ViewPager将水平 RecycleView 滑动事件调度到 ViewPager
【发布时间】:2018-01-29 21:33:25
【问题描述】:

在我的应用中,我有一个 ConstraintLayout。在这个布局中,我有两个主要元素:

  • ViewPager
  • 回收站视图

使用 ViewPager,您可以水平滑动以更改页面。 RecyclerView 是垂直的。现在,RecyclerView 消耗所有的滑动事件,包括水平滑动。 我希望能够在 RecyclerView 上执行水平滑动(向右或向左),它将分派到 ViewPager(即更改页面)。这可能吗?

到目前为止我尝试了什么:

  • 尝试在 RecyclerView 上创建侦听器,问题:由于这是一个垂直 RecyclerView,我没有收到水平滑动事件。

一些代码

xml 文件(缩短):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="my.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ffffffff"
        android:paddingEnd="30dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingStart="30dp">

        <com.gigamole.navigationtabstrip.NavigationTabStrip
            android:id="@+id/nts_center"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_gravity="center"
            app:nts_active_color="#42a4d1"
            app:nts_color="#42a4d1"
            app:nts_corners_radius="1dp"
            app:nts_inactive_color="#ff1a1e23"
            app:nts_size="15sp"
            app:nts_titles="@array/titles"
            app:nts_weight="3dp" />

    </FrameLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="55dp"
        android:scrollbars="vertical" />
</android.support.constraint.ConstraintLayout>

主活动

mViewPager = findViewById(R.id.vp);
mNavigationTabStrip = findViewById(R.id.nts_center);

list = new ArrayList<>();

mRecyclerView = findViewById(R.id.recycler_view);

customAdapter = new CustomAdapter(list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLongClickable(false);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setNestedScrollingEnabled(true);
mRecyclerView.setAdapter(customAdapter);

【问题讨论】:

  • 您将RecyclerView 放置在您的ViewPager 之上,它们打算如何相互关联?
  • 感谢您的评论。 RecyclerView 在 ViewPager 下。这正是它们的定位方式:在 ViewPager 的最顶部和 RecyclerView 的正下方(与显示的其余部分一起)。 Like this
  • 这不是您的布局显示的内容。 RecyclerViewViewPager 的高度和宽度都设置为match_parent,然后您的NavigationTabStrip 被绘制在同一区域上。最终结果是您的ViewPager 完全位于您的RecyclerViewFrameLayout 下方,其中包含您的NavigationTabStripViewPager 没有接收到触摸信息,因为默认情况下它很可能被 RecyclerView 使用。
  • @BryanDormaier 非常感谢您的帮助!很抱歉对布局的误解。但我仍然无法正确处理。我尝试了两件事:1)不使用recycleviewer的触摸事件(不起作用,因为我首先没有获得水平滑动)2)尝试从recyclerview和viewpager中删除两个侦听器并添加一个侦听器从那里委托单击事件的约束布局(问题:我没有设法创建这样的侦听器,约束布局侦听器从未被调用)。更好的方法是什么?

标签: android android-recyclerview android-viewpager


【解决方案1】:

使用

         LinearLayoutManager horizontalLayoutManager = new 
         LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

创建你的水平recyclerView

【讨论】:

    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2023-03-27
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多