猿问

水平 RecyclerView 内的水平 RecyclerView

我正在尝试在另一个带有 Horizontal LinearLayoutManager 的 RecyclerView 中使用带有 Horizontal LinearLayoutManager 的 RecyclerView。整个层次结构是这样的:RecyclerView,Recycler 的子元素是一个 ScrollView,它包含一个 TextView 和另一个 RecyclerView。为了更好地理解,我希望第一个回收器的工作方式类似于但不完全相同的 ViewPager(我不想使用 ViewPager)。问题是当我尝试在子回收器上水平滚动时,运动事件被父回收器捕获,导致滚动到下一页而无法滚动子回收器。


主活动布局:


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 

    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:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <android.support.v7.widget.RecyclerView

        android:id="@+id/RVpage"

        android:descendantFocusability="blocksDescendants"

        android:focusableInTouchMode="false"

        android:focusable="false"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />


</LinearLayout>

主活动.java:


public class MainActivity extends AppCompatActivity {


    private RecyclerView recyclerView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        setRecycler();

    }


    private void setRecycler() {

        recyclerView=(RecyclerView) findViewById(R.id.RVpage);

        SnapHelper snapHelper = new LinearSnapHelper();

        snapHelper.attachToRecyclerView(recyclerView);

        BigRecyclerAdapter bigRecyclerAdapter=new BigRecyclerAdapter(this);

        recyclerView.setAdapter(bigRecyclerAdapter);


        //recyclerView.setNestedScrollingEnabled(true);



        recyclerView.setLayoutManager(new LinearLayoutManager(

            this, LinearLayoutManager.HORIZONTAL, false)

        );

    }

}


翻过高山走不出你
浏览 231回答 2
2回答

慕斯王

我在嵌套ScrollView之前实现过,我认为子滚动视图总是消耗触摸,而父滚动视图永远不会听它们这是 onInterceptTouchEvent控制视图是消耗触摸还是将其传递给其父级的方法的作用。所以计划是计算滚动的方向,我是否到达了子 ScrollView 的滚动结束。这段代码是 2 年前写的,所以如果有些东西被弃用了,请原谅。public class CustomScrollView extends ScrollView {private boolean bottomReached = false;private boolean topReached = true;private float startTouch = -1;private float distance = -1;public CustomScrollView(Context context) {&nbsp; &nbsp; super(context);}public CustomScrollView(Context context, AttributeSet attrs) {&nbsp; &nbsp; super(context, attrs);}public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {&nbsp; &nbsp; super(context, attrs, defStyleAttr);}@Overrideprotected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {&nbsp; &nbsp; super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {&nbsp; &nbsp; switch (ev.getAction()) {&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTouch = ev.getY();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTouch = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_MOVE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; distance = ev.getY() - startTouch;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Math.abs(distance) < 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean onIntercept = super.onInterceptTouchEvent(ev);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return onIntercept;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!bottomReached && !topReached) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (distance > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Scrolling Up&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return bottomReached;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Scrolling Down&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return topReached;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return super.onInterceptTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {&nbsp; &nbsp; switch (ev.getAction()) {&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTouch = ev.getY();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTouch = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_MOVE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; distance = ev.getY() - startTouch;&nbsp; &nbsp; }&nbsp; &nbsp; return super.onTouchEvent(ev);}}

慕田峪7331174

看看这个站点和他在 GitHub&nbsp;Nested Recyclerview上的代码
随时随地看视频慕课网APP

相关分类

Java
我要回答