【问题标题】:Nested ScrollView with a Fragment and RelativeLayout (containing RecyclerView) not scrolling带有 Fragment 和 RelativeLayout(包含 RecyclerView)的嵌套 ScrollView 不滚动
【发布时间】:2020-01-14 16:32:52
【问题描述】:

我有一个带有 NestedScrollView 的布局,其中包含一个 LinearLayout。

这个LinearLayout包含一个Fragment(FrameLayout作为容器)和一个包含RecyclerView的RelativeLayout。

问题是只有 RecyclerView 正在滚动,并且项目在片段下方。

理想情况下,片段应与 recyclerview 一起向上滚动。我尝试在 recyclerview 上设置 nestedScrollingEnabled = false 但这会停止所有滚动(即使对于 recyclerview 也是如此)。

这是布局:

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

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:list="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">


    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:overScrollMode="never">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:orientation="vertical">

            <FrameLayout
                android:id="@+id/header_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:orientation="vertical" />

            <RelativeLayout
                android:id="@+id/paginated_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/header_container"
                android:paddingLeft="@dimen/card_margin"
                android:paddingRight="@dimen/card_margin">

                   ... swiperefreshlayout, recyclerview here.

              </RelativeLayout>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</layout>

谁能告诉我怎么了?

【问题讨论】:

    标签: android android-layout android-fragments android-recyclerview


    【解决方案1】:

    试试这个:

    VerticalScrollview.java

    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.widget.ScrollView;
    
    public class VerticalScrollview extends ScrollView {
    
        public VerticalScrollview(Context context) {
            super(context);
        }
    
        public VerticalScrollview(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            final int action = ev.getAction();
            switch (action)
            {
                case MotionEvent.ACTION_DOWN:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                    super.onTouchEvent(ev);
                    break;
    
                case MotionEvent.ACTION_MOVE:
                    return false; // redirect MotionEvents to ourself
    
                case MotionEvent.ACTION_CANCEL:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                    super.onTouchEvent(ev);
                    break;
    
                case MotionEvent.ACTION_UP:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                    return false;
    
                default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
            }
    
            return false;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            super.onTouchEvent(ev);
            Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
            return true;
        }
    }
    

    xml布局文件:

    <com.core.views.VerticalScrollview
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:overScrollMode="never">
    
            //YOUR VIEWS
    
    </VerticalScrollview>
    

    【讨论】:

    • 你为什么给它一个权重1和高度0?是家长。它不应该被包裹在一个线性布局中,或者是吗?
    猜你喜欢
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多