RecyclerView嵌套在ViewPager和NestedScrollView中的CoordinatorLayout中,但无法滚动。

3
我得到了以下结构:
<CoordinatorLayout>
  <AppBar> 
    <CollapsingToolbar             
     app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"/>
  </AppBar>
  <NestedScrollView
        android:layout_height="wrap_content"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <ViewPager />
  </NestedScrollView>
</CoordinatorLayout>

ViewPager中的片段如下:

<LinearLayout
  android:layout_height="wrap_content">
  <RecyclerView
      android:layout_height="wrap_content" />
</LinearLayout>

在这个片段的RecyclerView中,我得到了

<RelativeLayout
  android:layout_height="wrap_content">
  <...>
  <RecyclerView
    ....
    android:layout_height="wrap_content" />
</RelativeLayout>

现在的问题是,当我向下滚动列表时,工具栏会正确地折叠。但当工具栏完全折叠时,就不再滚动了,我无法想出如何实现这一点。
对于每个RecyclerView,我都使用一个启用了AutoMeasureEnabled的LinearLayoutManager。此外,HasFixedSize标志设置为false。
有什么想法吗?
//编辑:内容不固定;它通过reactiveUI传递进来。

将 Fragment 的 XML 布局中的 <LinearLayout> 更改为 <NestedScrollView>。 - Divyesh Patel
我应该改变什么? - Florian Hansen
在 Fragment 的 XML 代码中,将 <NestedScrollView> 设为父布局,并在其中添加 LinearLayout。 - Divyesh Patel
你可以从协调布局中删除 <NestedScrollView>。 - Divyesh Patel
1
可能是如何将RecyclerView放入NestedScrollView中?的重复问题。 - Bishoy Kamel
显示剩余2条评论
3个回答

1

您需要制作具有动态高度的视图分页器,我在NestedScrollView中使用了以下自定义视图分页器:

public class CustomPager extends ViewPager {

private View mCurrentView;

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

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

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mCurrentView == null) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;
    }
    int height = 0;
    mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int h = mCurrentView.getMeasuredHeight();
    if (h > height) height = h;
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void measureCurrentView(View currentView) {
    mCurrentView = currentView;
    requestLayout();
}

public int measureFragment(View view) {
    if (view == null)
        return 0;

    view.measure(0, 0);
    return view.getMeasuredHeight();
}
}

并将以下方法覆盖到您的FragmentStatePagerAdapter或FragmentPagerAdapter中

private int mCurrentPosition = -1;
@Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        if (position != mCurrentPosition) {
            Fragment fragment = (Fragment) object;
            CustomPager pager = (CustomPager) container;
            if (fragment != null && fragment.getView() != null) {
                mCurrentPosition = position;
                pager.measureCurrentView(fragment.getView());
            }
        }
    }

你能提供一个样例吗? - hushed_voice

0

CollapsingToolbar布局滚动设置此属性

app:layout_scrollFlags="scroll|exitUntilCollapsed"

0

做出这样的更改,这样就可以工作了

    <CoordinatorLayout>
  <AppBar> 
    <CollapsingToolbar             
     app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"/>
  </AppBar>
    <ViewPager
          app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</CoordinatorLayout>

在你的片段布局中,将父级设置为NestedScrollView,它就可以工作了。


仍然不起作用 :/ 我的VP现在已经应用了这个行为。我的Fragment看起来像这样 <NestedScrollView> <RecyclerView/> </NestedScrollView>内部列表仍然是相同的。这也应该是一个<NestedScrollView>根吗? - Florian Hansen

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