我得到以下结构:

<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,我正在使用LinearLayoutManager,并将AutoMeasureEnabled设置为true。此外,HasFixedSize-Flags设置为false。

有任何想法吗?

//编辑:内容不固定;它通过reactUI传入。

最佳答案

您必须使视图寻呼机具有动态高度,我在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());
            }
        }
    }

07-24 09:47
查看更多