国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

NestedScrollView+Recyclerview下滑卡頓解決方法

2019-10-22 18:23:02
字體:
來源:轉載
供稿:網友

大家在進行安卓開發用到NestedScrollView+Recyclerview的時候,經常出現的情況就是加載下滑的時候沒有任何問題,很流暢,但是在下滑以后明顯出現了卡頓的情況,小編根絕這個問題,給大家再來的解決方法,一起來學習下。

我們先來看下這個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);

 

以上是小編測試過的解決方法,接下來,我們再給大家分享一篇簡單的方法代碼:

 最開始使用ScrollView的時候嵌套ListView會出現item顯示不全等一些問題,現在google提供NestedScrollView已經可以解決該問題,但是在使用NestedScrollView嵌套RecyclerView的時候會發現我們在RecyclerView上滑動的時候沒有了滾動的效果,查看文檔找到的解決辦法:

 LinearLayoutManager layoutManager = new LinearLayoutManager(this);  layoutManager.setSmoothScrollbarEnabled(true);  layoutManager.setAutoMeasureEnabled(true);  recyclerView.setLayoutManager(layoutManager);  recyclerView.setHasFixedSize(true);  recyclerView.setNestedScrollingEnabled(false);

就在小編完稿的時候,又發現了兩種方法,大神真的是多啊,一起整理后分享給你

當ScrollView嵌套RecyclerView時,會出現滑動卡頓,不平滑的效果。對此有兩種解決方案。

方案一

設置RecyclerView屬性方法

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

或者直接在recycleview中 添加屬性

android:nestedScrollingEnabled="false"


方案二

如果方案一無效,不妨試試重寫ScrollView的onInterceptTouchEvent()方法,強制讓其觸摸事件都交給其子控件去處理

public class RecycleScrollView extends ScrollView {private int downX;private int downY;private int mTouchSlop;public RecycleScrollView(Context context) {super(context);mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();}public RecycleScrollView(Context context, AttributeSet attrs) {super(context, attrs);mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();}public RecycleScrollView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();}@Overridepublic boolean onInterceptTouchEvent(MotionEvent e) {int action = e.getAction();switch (action) {case MotionEvent.ACTION_DOWN:downX = (int) e.getRawX();downY = (int) e.getRawY();break;case MotionEvent.ACTION_MOVE:int moveY = (int) e.getRawY();if (Math.abs(moveY - downY) > mTouchSlop) {return true;}}return super.onInterceptTouchEvent(e);}}

通過以上操作,界面就不會再卡頓了,還原了原本的慣性。

以上就是關于NestedScrollView+Recyclerview下滑卡頓的所有方法,希望我們整理的東西能夠真正幫助到你,喜歡的話就收藏一下吧。

 

注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建瓯市| 公主岭市| 离岛区| 三穗县| 普兰县| 台东县| 哈巴河县| 万山特区| 秀山| 安图县| 自贡市| 宜兰县| 炎陵县| 新田县| 吴忠市| 专栏| 同心县| 成武县| 安吉县| 嘉义县| 宁安市| 罗平县| 东宁县| 安陆市| 台东市| 山阴县| 滕州市| 屏山县| 海淀区| 柳河县| 通山县| 襄城县| 崇阳县| 那坡县| 江陵县| 都江堰市| 广东省| 大渡口区| 乌兰县| 扶绥县| 青铜峡市|