使用SwipRecyclerView代替NestedScrollView解决滑动卡顿问题

总结一个界面性能优化的方法

  • 需求是,将整个页面当成一个整体可以上下滑动,界面包含了两段布局和一个列表。
  • 旧的布局和问题:布局由上下两段组成(上面的两个输入框和一个相对布局,下面是一个RecyclerView),放到一个NestedScrollView中形成可以上下滑动的整体布局。这样有个弊端,当RecyclerView中的数据比较多的时候比如超过五十条item,那么整体滑动起来不顺畅,甚至手指离开滚动就停止,当用户与item中输入框交互时候变得十分卡顿。

先上布局图片:

edit-word.jpg

布局代码

    <android.support.v4.widget.NestedScrollView
        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="wrap_content"
                android:layout_marginHorizontal="16dp"
                android:orientation="vertical">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="8dp">

                    <EditText
                        android:id="@+id/book_name_edit"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/book_title" />

                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="16dp">

                    <EditText
                        android:id="@+id/book_introduction_edit"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/book_instruction" />

                </android.support.design.widget.TextInputLayout>
            </LinearLayout>


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="16dp"
                android:layout_marginVertical="8dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:text="@string/word_list"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                    android:textColor="#000" />

                <Button
                    android:id="@+id/translate_btn"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="0dp"

                    android:background="@drawable/ic_translate_black_30dp"
                    android:focusable="false"
                    android:onClick="TranslateClick"
                    android:padding="12dp" />

            </RelativeLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/edit_book_recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

之前的解决方案

在网上查阅了很多网友总结的解决方案大致是以下情况比如设置他的两条属性:

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

一条是让item的尺寸大小收到adapter影响,一个是关闭Recyclerview的滑动,使用scrolview来进行滑动,这样当数据量小的时候确实可以做到滑动平滑。
这样的属性一旦item数目较大(亲测超过50个时候)就会变得卡顿,尤其是拉倒最底部然后再向上滑动时候,出现卡顿,FPS可谓十分低,更不用说让用户和item交互了进行文本输入以及其他交互了。
这样的原理是,当下拉加载时候将item全部都加载完毕,然后将布局作为一个整体来上下滑动(滑动需要加载全部控件),这完全牺牲了RecyclerView原本的自动回收机制,数据多仍使得性能低下。

我的解决方案

既然想让整体上下滑动,又要有RecyclerView的自动回收机制,那么就使用一个控件来解决需求即可。将Recyclerview作为滑动的控件,将上部分作为一个整体当成一个item,作为headerView加在Recyclerview的头部。这样一来,整体就是一个高性能的Recyclerview了,而且布局样式一点都没有收到影响。如图:


image.png

我使用的是严振杰大佬的开源控件SwipRecyclerView,地址为:SwipeRecyclerView

  • 首先需要将控件集成到项目
 //多功能的RecyclerView
    implementation 'com.yanzhenjie.recyclerview:support:1.3.2'
  • 修改后的布局代码
    红框内的布局,单独创建一个新布局文件header_layout.xml,将之前布局上方部分抠出来单独放进去即可。
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="8dp">

                <EditText
                    android:id="@+id/book_name_edit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/book_title" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="16dp">

                <EditText
                    android:id="@+id/book_introduction_edit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/book_instruction" />

            </android.support.design.widget.TextInputLayout>
        </LinearLayout>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="16dp"
            android:layout_marginVertical="8dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="@string/word_list"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                android:textColor="#000" />

            <Button
                android:id="@+id/translate_btn"
                android:layout_width="24dp"
                android:layout_height="24dp"

                android:layout_alignParentRight="true"
                android:layout_marginRight="0dp"

                android:background="@drawable/ic_g_translate_black_24dp"
                android:focusable="false"
                android:onClick="TranslateClick"
                android:padding="12dp" />

        </RelativeLayout>


</LinearLayout>

将之前的Recyclerview使用SwipRecyclerView替换如下,抠出来headerView然后修改之前的布局为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.EditBookActivity">

  
    <com.yanzhenjie.recyclerview.SwipeRecyclerView
        android:id="@+id/edit_book_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
  • 实现
    这样一来看起来简洁了许多接下来就是代码实现。注意使用的控件为SwipRecyclerView(和RecyclerView是父子类关系)
     //其他初始化操作获取控件等
      .
      .
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new EditBookWordsAdapter(this, wordsInfoList);
        recyclerView.setAdapter(adapter);

        //将上部分布局改为headrview
        View headerView=getLayoutInflater().inflate(R.layout.edit_word_headerview,recyclerView,false);
        //获取到headerview中的子控件
        nEditText=headerView.findViewById(R.id.book_name_edit);
        iEditText=headerView.findViewById(R.id.book_introduction_edit);
        translate_btn=headerView.findViewById(R.id.translate_btn);
        //获取到控件添加自己其他操作
        nEditText.setText(book.getName());
        iEditText.setText(book.getIntroduction());
       //最后使用addHeaderView添加到Recyclerview
        recyclerView.addHeaderView(headerView);
  • 特别注意,添加一个HeaderView,凡是通过ViewHolder拿到的position都要减去添加的HeaderView 的数量才能正确获取到position,这在原文档也强调了。
    另外也提供了其他的一些方法
addHeaderView(View); // 添加HeaderView。
removeHeaderView(View); // 移除HeaderView。
addFooterView(View); // 添加FooterView。
removeFooterView(View); // 移除FooterView。
getHeaderItemCount(); // 获取HeaderView个数。
getFooterItemCount(); // 获取FooterView个数。
getItemViewType(int); // 获取Item的ViewType,包括HeaderView、FooterView、普通ItemView。
//添加/移除HeaderView/FooterView和setAdapter()的调用不分先后顺序。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 227,663评论 6 531
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,125评论 3 414
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 175,506评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,614评论 1 307
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,402评论 6 404
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,934评论 1 321
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,021评论 3 440
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,168评论 0 287
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,690评论 1 333
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,596评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,784评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,288评论 5 357
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,027评论 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,404评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,662评论 1 280
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,398评论 3 390
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,743评论 2 370