java - ViewPgaer嵌套多个fragment里面嵌套NestedScrollview的问题
迷茫
迷茫 2017-04-18 10:28:00
0
1
602

最外层是像微信那样的效果,ViewPager里面放了几个Fragment。
Fragment里要使用RecyclerView,但是在RecyclerView上面还有其他的东西,所以我把它们放在LinearLayout里面,再把LinearLayout放在ScrollView里面。这样便实现了我要的效果。
ViewPager可以正常地水平滑动切换,RecyclerView也可以正常地垂直滑动。
问题是:
不管是滑动切换ViewPager还是直接点击下面的按钮切换(就像微信),当切换动画结束,并且RecyclerView部分可见时,如果这时RecyclerView上边缘低于屏幕的上边缘,ScrollView就会跳动一下,RecyclerView的上边缘就正好挨着屏幕上边缘了。
切换过程中RecyclerView不可见时不会发生跳动。
切换过程中RecyclerView上边缘高于屏幕上边缘时,不会发生跳动。
其他原因导致的RecyclerView可见,如Activity的跳转,不会发生跳动。

Fragment布局片段

<NestedScrollview

android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Text"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcv_draw_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_bottom_line"
        />
</LinearLayout>

</NestedScrollview>

...
可能有人说添加头布局,但是我添加头布局之后会出问题,左右宽度会压缩,我如果不添加头布局还有什么解决方式吗?急求,谢谢。如果有人遇到这样的问题又解决了的话请加QQ283006603

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
PHPzhong

不建议ScrollView里面嵌套RecyclerView,在计算高度的时候会出问题,性能也是问题,还是推荐用使用头type,RecyclerView宽度铺满,左右宽度在item里面控制

你硬是要那么做的话试着重写RecyclerView的onMeasure方法看看

@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
  }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板