继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

NestedScrollView+Recyclerview 滑动卡顿,惯性

喵喔喔
关注TA
已关注
手记 388
粉丝 101
获赞 605

bug
1.滑动卡顿,
2.加载下滑时流畅,下滑时明显的卡顿
3.进入页面时直接加载RecyclerView部分的内容(这里我理解为控件惯性,不知道对不对-------尴尬!!!!!!)

下面我们一一来解决这些问题


在开发项目中,涉及到到商品详情页,新闻详情页等的页面时,通常情况下,商品详情页的底部会附上商品的评论或者是相关商品的的推荐,或者是相关性的文章.那么我们就会用到列表的RecyclerView,在头部可能是一些比较复杂的多种界面,可能采用比较简单的方法来处理,那就是NestedScrollView+Recyclerview,这这种方式比较直观和方便操作.比如像下面的代码

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView_comment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 .....此处省略      
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:gravity="center">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@color/text_msg_33"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:text="1"
                    android:textColor="#8c8c8c"
                    android:textSize="15sp"/>

                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@color/text_msg_33"/>
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/bg_shop_card"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:text="加入购物车"
                android:textColor="@color/white"
                android:textSize="14sp"/>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="#f2f2f2"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="11dp"
                android:text="用户评价"
                android:textColor="#666666"
                android:textSize="13sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/line_1px"
                android:layout_marginRight="20dp"
                android:text="(21313)"
                android:textColor="#666666"
                android:textSize="13sp"/>

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#dcdcdc"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_seller_comment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants"
            android:nestedScrollingEnabled="false"
          />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查看更多"
            android:textColor="#8c8c8c"
            android:textSize="13sp"/>
    </LinearLayout></android.support.v4.widget.NestedScrollView>

首先.滑动动卡顿的问题.
在布局文件中添加

android:nestedScrollingEnabled="false"

这一属性
或者通过代码设置也是可以的,

mRecycler.setNestedScrollingEnabled(false);

这样滑动的时候就不会出现有卡顿的现象.
其次是加载上下滑动加载流畅时
通过代码

mRecycler.setHasFixedSize(false);

对于第三种现象,我找了很多方法,都以失败而告终,其实出现这种情况是应为Recyclerview在加载数据的时候获取到了焦点导致,所以只需要在对RecylerView在带中设置不能获取焦点即可.
添加以下代码
mRecycler.setFocusable(false);

NestedScrollView其他问题

百度地图+NestedScrollView的滑动冲突的问题解决地址

原文链接:http://www.apkbus.com/blog-632845-76512.html

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP