項目中時常需要實現(xiàn)在ScrollView中嵌入一個或多個RecyclerView。這一做法通常會導致如下幾個問題
- 頁面滑動卡頓
- ScrollView高度顯示不正常
- RecyclerView內(nèi)容顯示不全
本文將利用多種方式分別解決上述問題
滑動卡頓解決方案
若只存在滑動卡頓這一問題,可以采用如下兩種簡單方式快速解決
利用RecyclerView內(nèi)部方法
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
其中辱魁,setHasFixedSize(true)方法使得RecyclerView能夠固定自身size不受adapter變化的影響陨闹;而setNestedScrollingeEnabled(false)方法則是進一步調(diào)用了RecyclerView內(nèi)部NestedScrollingChildHelper對象的setNestedScrollingeEnabled(false)方法锤躁,如下
public void setNestedScrollingEnabled(boolean enabled) {
getScrollingChildHelper().setNestedScrollingEnabled(enabled);
}
進而,NestedScrollingChildHelper對象通過該方法關閉RecyclerView的嵌套滑動特性红柱,如下
public void setNestedScrollingEnabled(boolean enabled) {
if (mIsNestedScrollingEnabled) {
ViewCompat.stopNestedScroll(mView);
}
mIsNestedScrollingEnabled = enabled;
}
如此一來遭庶,限制了RecyclerView自身的滑動,整個頁面滑動僅依靠ScrollView實現(xiàn)稠屠,即可解決滑動卡頓的問題
重寫LayoutManager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
@Override
public boolean canScrollVertically() {
return false;
}
};
這一方式使得RecyclerView的垂直滑動始終返回false峦睡,其目的同樣是為了限制自身的滑動
綜合解決方案
若是需要綜合解決上述三個問題翎苫,則可以采用如下幾種方式
插入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/tianzhijiexian/p/4469516.html
重寫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當前的觸摸事件
* @param ev 觸摸事件
* @return true:調(diào)用onMotionEvent()方法转捕,并完成滑動操作
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// 保存當前touch的縱坐標值
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);
}
/**
* 獲取相應context的touch slop值(即在用戶滑動之前,能夠滑動的以像素為單位的距離)
* @param context ScrollView對應的context
*/
private void setSlop(Context context) {
slop = ViewConfiguration.get(context).getScaledTouchSlop();
}
}
事實上五芝,盡管我們能夠采用多種方式解決ScrollView嵌套RecyclerView所產(chǎn)生的一系列問題痘儡,但由于上述解決方式均會使得RecyclerView在頁面加載過程中一次性顯示所有內(nèi)容,因此當RecyclerView下的條目過多時枢步,將會對影響整個應用的運行效率沉删。基于此醉途,在這種情況下我們應當盡量避免采用ScrollView嵌套RecyclerView的布局方式