提问者:小点点

将水平RecycleView滑动事件调度到ViewPager


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

  • 查看页面
  • 回收者视图

使用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);

共1个答案

匿名用户

使用

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

以创建水平回收器视图