Android Scrollview嵌套RecycleView滑动不流畅,卡顿问题

最近在做项目时,需要仿QQ那样的弹性动画效果。于是就用ScrollView加RecycleView开始了。

<com.dten.assistant.ui.view.MyScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white">
             <ImageView
                android:layout_below="@+id/ll_search"
                android:id="@+id/iv_line"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:src="@drawable/ic_content_line"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="20dp"/>
            <android.support.v7.widget.RecyclerView
                android:layout_below="@+id/iv_line"
                android:id="@+id/rcv_selector_contents"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

大概布局就是这样的,完成之后,发现一个问题,就是在滑动的不流畅,卡顿,基本就是手指移动多长,界面滑动多长距离。于是找资料,在recycleview添加这两个方法就可以了。

recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);

于是加上之后赶紧试了试,可是不对啊。。。。发现再向下滑动的时候划不动,这可怎么办呢。
原来我的布局是

<com.dten.assistant.ui.view.MyScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

ScrollView是最外层的,于是在ScrollView外层再包裹一层布局即可

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F9F9F9">
    <com.dten.assistant.ui.view.MyScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

现在就可以滑动了,而且很流畅,有滑动惯性。

另外:如果Listview或者RecycleView显示不全,只有一个itme,请在ScrollView中添加

android:fillViewport="true"

在Android 5.0后,滑动到底部或者顶部会有一个月牙形的影印,有些场景可能会有点不好看,去掉这个:

android:overScrollMode= "never"
android:fadingEdge="none"

欢迎大家批评指正。

猜你喜欢

转载自blog.csdn.net/qq_34983989/article/details/78133711