RecyclerView里面的view-pager在scrollview里面

时间:2017-07-24 11:13:26

标签: android

我有这个布局>

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:fillViewport="true">

        <LinearLayout
            android:id="header_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
             >
            <android.support.v7.widget.RecyclerView
                android:id="@+id/moods_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:scrollbars="none" />

            <android.support.v4.view.ViewPager
                android:id="@+id/mainwindow_view_pager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>

    </ScrollView>


    <android.support.design.widget.TabLayout
        android:id="@+id/mainwindow_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@drawable/view_border_top" />
</LinearLayout>

一个ViewPager Fragment有这个代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/net_rclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:layout_weight="0"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>
</LinearLayout>

我想要

  

header_content布局

时滚动出来

  在view-pager片段

中的

net_rclerview

滚动。 问题是recyclerview正在滚动,但上面的内容没有滚动出视图,这是否可以实现,如果有任何其他选项我可以做?

2 个答案:

答案 0 :(得分:2)

我刚遇到类似的问题,经过大量的冲浪后,我发现问题不在于RecyclerView,而在于ViewPage r。放入ScrollView时, ViewPager的WrapContent属性不起作用。因此固定高度限制RecyclerView显示完整内容。只需更新ViewPager的OnMeasure方法如下:

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

使用NestedScrollView代替ScrollView

 <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollingView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:fitsSystemWindows="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

RecyclerView.setNestedScrollingEnabled(false);发送到recyclerview

相关问题