【问题标题】:How to scroll RecyclerView inside a ViewPager?如何在 ViewPager 中滚动 RecyclerView?
【发布时间】:2016-11-29 09:58:57
【问题描述】:

我在 ViewPager 中有两个选项卡,每个选项卡都包含一个 RecyclerView,它们基本上是垂直的 ListView。我的问题是我无法垂直滚动 RecyclerViews。

 <com.myapplication.MyViewPager
    android:layout_below="@+id/music_tabs"
    android:id="@+id/music_switcher"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">



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


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

</com.myapplication.MyViewPager>

这里是 MyViewPager

public class MyViewPager extends ViewPager {

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int height = 0;
    for(int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if(h > height) height = h;
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

【问题讨论】:

标签: android android-viewpager android-recyclerview


【解决方案1】:

似乎是我的 onMeasure 方法导致了问题。删除后问题解决。

【讨论】:

    【解决方案2】:

    您无法事先测量 recyclerview 的大小。正如预期的那样,删除 onMeasure 后它应该可以工作。

    【讨论】:

      【解决方案3】:

      当您使用两个 recycleview 并且布局高度是包装内容而不是使用此代码或在 dp 中定义 layout_height 时:

      android:layout_height="250dp"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-11
        • 1970-01-01
        • 1970-01-01
        • 2016-05-07
        • 2017-11-24
        • 1970-01-01
        相关资源
        最近更新 更多