RecyclerView在ViewPager中嵌套ScrollView无法正常工作

15

我有一个包含以下视图的xml布局:Scrollview->RelativeLayout->Some Views + Tablayout + ViewPager->Recylerview(位于ViewPager的片段中)。ViewPager有一些固定高度(将其设置为"Wrap_Content"根本不会显示它)。现在问题是Recylerview永远不会滚动。我已经尝试了几种已发布的解决方案,例如将viewpager包装在"Nested Scrollview"中,甚至使其成为LinearLayout的子级。但是没有任何作用。

以下是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btn_startdraw"
        android:fillViewport="true"
        android:gravity="center">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:padding="@dimen/_5sdp">


            <TextView
                android:id="@+id/invites_accepted"
                style="@style/bodyStyleBlue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/committee_slots"
                android:text="@string/invites_accepted" />

               <!-- A lot of other Textfields for data display-->

                <android.support.design.widget.TabLayout
                android:id="@+id/tabs_pendingcommittee"
                style="@style/TabStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/draw_mode_val"
                app:tabMode="fixed"></android.support.design.widget.TabLayout>b


            <com.apponative.committeeapp.ui.custom.CustomViewPager
                android:id="@+id/pending_member_view"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_250sdp"
                android:layout_below="@+id/tabs_pendingcommittee"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>

    <!-- Some other views on bottom-->
</RelativeLayout>

ViewPager正在显示的片段具有以下布局XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <TextView
        style="@style/bodyStyleBold1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="No People..." />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/contact_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:background="@color/white"></android.support.v7.widget.RecyclerView>
</FrameLayout>

我尝试过在Recyclerview上使用nestedScrollingEnabledsetFixedHeight属性。但是都没有起作用。


1
我认为你应该从相对布局中删除滚动视图,因为Recycler View本身就有自己的滚动条。 - Rounak Lahoti
1
@RounakLahoti 我的布局有很多需要滚动的视图。我没有使用ScrollView,而是使用RecyclerView。RecyclerView也是布局中的一个子项,它显示了除其他视图(TextView、ImageView、EditView的组合)之外的另一个数据列表。 - Saira Nawaz
2个回答

22
我刚刚发现问题不是出现在ViewPager或ScrollView内的RecyclerView上,而是出现在ScrollView内的ViewPager上。当我将ViewPager放入ScrollView时,它的Wrap_Content属性无法正常工作,因此固定高度会限制RecyclerView内容的完全显示。所以我通过自定义ViewPager的onMeasure方法来解决这个问题,并且它已经平稳地工作了。不需要使用NestedScroll view或其他给定的解决方案。
以下是我自定义的ViewPager代码,当作为ScrollView的子项添加时,它可以包裹内容。
public class CustomViewPager extends ViewPager {

    boolean canSwipe = true;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        View child = getChildAt(getCurrentItem());
        if (child != null) {
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void canSwipe(boolean canSwipe) {
        this.canSwipe = canSwipe;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.canSwipe) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.canSwipe) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }
}

我已将自定义视图页面的高度设置为默认的wrap_content。我认为你的代码会根据子项(recyclerview)的高度重新计算高度。对吗? - SourabhMore
@SourabhMore 是的,它会重新计算子项高度并重新分配给视图翻页器。 - Saira Nawaz
如何使用ViewPager 2实现这个功能? - Sharif Rafid Ur Rahman

0

添加 NestedScrollView 并设置 recyclerView.setNestedScrollingEnabled(false);

检查您已经有了这个 android.support.v7.widget.RecyclerView

请记住,RecyclerView 元素有自己的滚动条

 <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

使用android.support.v4.widget.NestedScrollView替换ScrollView可在所有版本上运行。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        style="@style/ScrollBarStyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btn_startdraw"
        android:fillViewport="true"
        android:gravity="center">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:padding="@dimen/_5sdp">


            <TextView
                android:id="@+id/invites_accepted"
                style="@style/bodyStyleBlue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/committee_slots"
                android:text="@string/invites_accepted" />

               <!-- A lot of other Textfields for data display-->

                <android.support.design.widget.TabLayout
                android:id="@+id/tabs_pendingcommittee"
                style="@style/TabStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/draw_mode_val"
                app:tabMode="fixed"></android.support.design.widget.TabLayout>b


            <com.apponative.committeeapp.ui.custom.CustomViewPager
                android:id="@+id/pending_member_view"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_250sdp"
                android:layout_below="@+id/tabs_pendingcommittee"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>

    <!-- Some other views on bottom-->
</RelativeLayout>

我希望它能正常运行! :) 如果不起作用,请告诉我!我会试着找出原因 :) 祝你有美好的一天!


1
我使用了你告诉我的相同方法,但它根本不起作用。 - Saira Nawaz
我看到了你下面的帖子!我做了类似的事情,但我定制了ImageView!那么,你找到答案了吗?@SairaNawaz - Cristofer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接