首页 > 解决方案 > 带有 Fragment 和 RelativeLayout(包含 RecyclerView)的嵌套 ScrollView 不滚动

问题描述

我有一个包含一个 LinearLayout 的 NestedScrollView 布局。

这个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>

有人可以告诉我有什么问题吗?

标签: androidandroid-layoutandroid-fragmentsandroid-recyclerview

解决方案


尝试这个 :

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>

推荐阅读