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

最外层是像微信那样的效果,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);
  }
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板