NestedScrollView+Recyclerview下滑卡頓解決方法
大家在進(jìn)行安卓開發(fā)用到NestedScrollView+Recyclerview的時(shí)候,經(jīng)常出現(xiàn)的情況就是加載下滑的時(shí)候沒有任何問題,很流暢,但是在下滑以后明顯出現(xiàn)了卡頓的情況,小編根絕這個(gè)問題,給大家再來的解決方法,一起來學(xué)習(xí)下。
我們先來看下這個(gè)BUG的表現(xiàn):
1.滑動(dòng)卡頓,
2.加載下滑時(shí)流暢,下滑時(shí)明顯的卡頓
3.進(jìn)入頁面時(shí)直接加載RecyclerView部分的內(nèi)容(這里我理解為控件慣性,不知道對(duì)不對(duì)-------尷尬!!!!!!)
下面我們一一來解決這些問題
在開發(fā)項(xiàng)目中,涉及到到商品詳情頁,新聞詳情頁等的頁面時(shí),通常情況下,商品詳情頁的底部會(huì)附上商品的評(píng)論或者是相關(guān)商品的的推薦,或者是相關(guān)性的文章.那么我們就會(huì)用到列表的RecyclerView,在頭部可能是一些比較復(fù)雜的多種界面,可能采用比較簡(jiǎn)單的方法來處理,那就是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="用戶評(píng)價(jià)"
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>
首先.滑動(dòng)動(dòng)卡頓的問題.
在布局文件中添加
android:nestedScrollingEnabled="false"
這一屬性
或者通過代碼設(shè)置也是可以的,
mRecycler.setNestedScrollingEnabled(false);
這樣滑動(dòng)的時(shí)候就不會(huì)出現(xiàn)有卡頓的現(xiàn)象.
其次是加載上下滑動(dòng)加載流暢時(shí)
通過代碼
mRecycler.setHasFixedSize(false);
對(duì)于第三種現(xiàn)象,我找了很多方法,都以失敗而告終,其實(shí)出現(xiàn)這種情況是應(yīng)為Recyclerview在加載數(shù)據(jù)的時(shí)候獲取到了焦點(diǎn)導(dǎo)致,所
以只需要在對(duì)RecylerView在帶中設(shè)置不能獲取焦點(diǎn)即可.
添加以下代碼
mRecycler.setFocusable(false);
以上是小編測(cè)試過的解決方法,接下來,我們?cè)俳o大家分享一篇簡(jiǎn)單的方法代碼:
最開始使用ScrollView的時(shí)候嵌套ListView會(huì)出現(xiàn)item顯示不全等一些問題,現(xiàn)在google提供NestedScrollView已經(jīng)可以解決該問題,但是在使用NestedScrollView嵌套R(shí)ecyclerView的時(shí)候會(huì)發(fā)現(xiàn)我們?cè)赗ecyclerView上滑動(dòng)的時(shí)候沒有了滾動(dòng)的效果,查看文檔找到的解決辦法:
LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setSmoothScrollbarEnabled(true); layoutManager.setAutoMeasureEnabled(true); recyclerView.setLayoutManager(layoutManager); recyclerView.setHasFixedSize(true); recyclerView.setNestedScrollingEnabled(false);
就在小編完稿的時(shí)候,又發(fā)現(xiàn)了兩種方法,大神真的是多啊,一起整理后分享給你
當(dāng)ScrollView嵌套R(shí)ecyclerView時(shí),會(huì)出現(xiàn)滑動(dòng)卡頓,不平滑的效果。對(duì)此有兩種解決方案。
方案一
設(shè)置RecyclerView屬性方法
recyclerView.setHasFixedSize( true); recyclerView.setNestedScrollingEnabled(false);
或者直接在recycleview中 添加屬性
android:nestedScrollingEnabled="false"
方案二
如果方案一無效,不妨試試重寫ScrollView的onInterceptTouchEvent()方法,強(qiáng)制讓其觸摸事件都交給其子控件去處理
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();
}
@Override
public 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);
}
}
通過以上操作,界面就不會(huì)再卡頓了,還原了原本的慣性。
以上就是關(guān)于NestedScrollView+Recyclerview下滑卡頓的所有方法,希望我們整理的東西能夠真正幫助到你,喜歡的話就收藏一下吧。
相關(guān)文章
android 限制某個(gè)操作每天只能操作指定的次數(shù)(示例代碼詳解)
這篇文章主要介紹了android 限制某個(gè)操作每天只能操作指定的次數(shù),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android自定義view實(shí)現(xiàn)動(dòng)態(tài)柱狀圖
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)動(dòng)態(tài)柱狀圖的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android Location服務(wù)之LocationManager案例詳解
這篇文章主要介紹了Android Location服務(wù)之LocationManager案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
android Setting中隱藏項(xiàng)實(shí)現(xiàn)原理與代碼
我們都知道做程序員有時(shí)會(huì)就像android中,程序員在setting中就隱藏這樣一項(xiàng),接下來將詳細(xì)介紹,感興趣的朋友可以了解下哦2013-01-01
安裝android開發(fā)環(huán)境原始版(windows版)
安裝android開發(fā)環(huán)境原始版(windows版)的詳細(xì)步驟2013-03-03
Android 中動(dòng)態(tài)加載.jar的實(shí)現(xiàn)步驟
本文介紹動(dòng)態(tài)加載 .jar的實(shí)現(xiàn)步驟,這將對(duì)你的android開發(fā)很有幫助,剛興趣的朋友可以了解下哦2013-01-01
Android+Flutter實(shí)現(xiàn)文字跑馬燈特效
跑馬燈常常被運(yùn)用在很多領(lǐng)域, 例如商場(chǎng)的電子條幅、大樓的宣傳廣告位、地鐵的廣告位。今天我們來說一下flutter 怎么通過繪制來實(shí)現(xiàn)跑馬燈效果!,希望對(duì)大家有所幫助2022-11-11

