直接上代碼:
recyclerView.setLayoutManager(new LinearLayoutManager(this){
@Override
public boolean canScrollVertically() {
//解決ScrollView里存在多個RecyclerView時滑動卡頓的問題
//如果你的RecyclerView是水平滑動的話可以重寫canScrollHorizontally方法
return false;
}
});
//解決數(shù)據(jù)加載不完的問題
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
//解決數(shù)據(jù)加載完成后, 沒有停留在頂部的問題
recyclerView.setFocusable(false);
. 切記,切記,切記, 重要的事情說三遍, 還解決不了的時候看這里
關(guān)于嵌套后滑動卡頓或者焦點之類的問題
使用了上面的方法還無法解決就把布局中的RecyclerView外層的
ScrollView換成NestedScrollView就可以解決了
大概就改成這樣
<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">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
綜合解決方案
若是需要綜合解決上述三個問題,則可以采用如下幾種方式
插入LinearLayout/RelativeLayout
在原有布局中插入一層LinearLayout/RelativeLayout,形成如下布局
重寫LayoutManager
該方法的核心思想在于通過重寫LayoutManager中的onMeasure()方法永丝,即
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
super.onMeasure(recycler, state, widthSpec, heightSpec);
}
重新實現(xiàn)RecyclerView高度的計算焚刚,使得其能夠在ScrollView中表現(xiàn)出正確的高度尽纽,具體重寫方式可參考這篇文章
http://www.cnblogs.com/tianzh...
重寫ScrollView
該方法的核心思想在于通過重寫ScrollView的onInterceptTouchEvent(MotionEvent ev)方法络拌,攔截滑動事件,使得滑動事件能夠直接傳遞給RecyclerView陶贼,具體重寫方式可參考如下
/**
* Created by YH on 2017/10/10.
*/
public class RecyclerScrollView extends ScrollView {
private int slop;
private int touch;
public RecyclerScrollView(Context context) {
super(context);
setSlop(context);
}
public RecyclerScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
setSlop(context);
}
public RecyclerScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setSlop(context);
}
/**
* 是否intercept當(dāng)前的觸摸事件
* @param ev 觸摸事件
* @return true:調(diào)用onMotionEvent()方法,并完成滑動操作
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// 保存當(dāng)前touch的縱坐標(biāo)值
touch = (int) ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
// 滑動距離大于slop值時待秃,返回true
if (Math.abs((int) ev.getRawY() - touch) > slop) return true;
break;
}
return super.onInterceptTouchEvent(ev);
}
/**
* 獲取相應(yīng)context的touch slop值(即在用戶滑動之前拜秧,能夠滑動的以像素為單位的距離)
* @param context ScrollView對應(yīng)的context
*/
private void setSlop(Context context) {
slop = ViewConfiguration.get(context).getScaledTouchSlop();
}
}
事實上,盡管我們能夠采用多種方式解決ScrollView嵌套RecyclerView所產(chǎn)生的一系列問題章郁,但由于上述解決方式均會使得RecyclerView在頁面加載過程中一次性顯示所有內(nèi)容枉氮,因此當(dāng)RecyclerView下的條目過多時,將會對影響整個應(yīng)用的運行效率暖庄×奶妫基于此,在這種情況下我們應(yīng)當(dāng)盡量避免采用ScrollView嵌套RecyclerView的布局方式