ViewPager里面的Recyclerview里面的scrollview无法正常工作

时间:2017-08-01 14:08:50

标签: android android-recyclerview android-viewpager android-tablayout

我有一个xml布局,其中包含以下视图 Scrollview-> RelativeLayout->一些视图+ Tablayout + ViewPager-> Recylerview(在ViewPager的片段中)。 ViewPager有一些固定的高度(保持“Wrap_Content”根本不显示它。)现在的问题是Recylerview永远不会滚动。我已经尝试了几个已经发布的解决方案,如Wraping viewpager在“Nested Scrollview”或者甚至让孩子成为 LinearLayout 。但没有工作。

下面是我的xml:

npm publish

Viewpager显示的片段有以下布局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>

我尝试在Recyclerview上使用 nestedScrollingEnabled 以及 setFixedHeight 属性。但是没什么用。

2 个答案:

答案 0 :(得分:10)

我刚刚发现问题不在 ViewPager ScrollView 中的 RecyclerView ,但问题出在 ViewPager ScrollView 中。当我在 ScrollView 中放置 ViewPager 时,其 Wrap_Content 属性无效,因此固定高度会限制RecyclerView内容完全显示。所以我通过自定义ViewPager的onMeasure 方法解决了这个问题,并且它运行顺畅。无需包装NestedScroll视图或其他给定的解决方案。

下面是我的CustomView寻呼机的代码,它在作为子项添加到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;
    }
}

答案 1 :(得分: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>

我希望它有效! :)如果它不起作用,请告诉我!我会试着找出它为什么不起作用:)祝你有愉快的一天!

相关问题