最近發現自己負責的項目中,有使用 ScrollView 嵌套 RecyclerView 的地方,但是沒有做任何針對滑動沖突的處理,于是就想看下為什么沒有做這個處理,便進行了如下測試,發現了幾個奇怪的問題。
測試場景:頁面內容包括類似 HeaderView 的部分 + RecyclerView列表部分,布局是垂直方向,此處列表之上的布局內容并不是以 header add到RecyclerView上的。
測試結果:
- 在部分手機上,如果列表內容過少,只會造成很小程度的滑動,這種滑動沖突是沒法察覺到的,很容易忽略滑動沖突;
- 在部分手機上,列表內容過多時,滑動 RecyclerView 部分,會很卡頓,滑動 僅ScrollView 部分,很順暢,筆者試過Vivio x5plus 5.0系統會出現這樣的情況;
- 在部分手機上,不管列表內容多少,當滑動時,只有 RecyclerView部分滑動,Header頂部布局內容固定,沒有一起滑動,Redmi Note4 6.0系統則會出現這種結果。
一般地,對于第三種結果,一看就知道不是我們想要的結果,這種便是滑動沖突,但是不仔細時,均會因前兩種結果而忽略了滑動沖突。現在,將針對以上的滑動沖突提供幾種不同的解決方法。
方式一:禁止RecyclerView滑動
最直接的方式是將布局管理器中判斷可滑動的方法,直接返回false,代碼如下:
LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
// 直接禁止垂直滑動
return false;
}
}
源碼實現:
/**
* @return true if {@link #getOrientation()} is {@link #VERTICAL}
*/
@Override
public boolean canScrollVertically() {
return mOrientation == VERTICAL;
}
另一種方式便是重寫布局管理器,以 LinearLayoutManager 為例,重寫滑動方法,并且通過外部手動設置,代碼如下:
public class ScrollLinearLayoutManager extends LinearLayoutManager {
private boolean isScrollEnable = true;
public ScrollLinearLayoutManager(Context context) {
super(context);
}
public ScrollLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public ScrollLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean canScrollVertically() {
return isScrollEnable && super.canScrollVertically();
}
/**
* 設置 RecyclerView 是否可以垂直滑動
* @param isEnable
*/
public void setScrollEnable(boolean isEnable) {
this.isScrollEnable = isEnable;
}
}
方式二:通過View事件分發機制,進行事件攔截
重寫父控件,讓父控件 ScrollView 直接攔截滑動事件,不向下分發給 RecyclerView,具體是定義一個ScrollView子類,重寫其 onInterceptTouchEvent()方法,代碼如下:
public class ScrollInterceptScrollView extends ScrollView {
private int downX, downY;
private int mTouchSlop;
public ScrollInterceptScrollView(Context context) {
this(context, null);
}
public ScrollInterceptScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ScrollInterceptScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ScrollInterceptScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downX = (int) ev.getRawX();
downY = (int) ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) ev.getRawY();
// 判斷是否滑動,若滑動就攔截事件
if (Math.abs(moveY - downY) > mTouchSlop) {
return true;
}
break;
default:
break;
}
return super.onInterceptTouchEvent(ev);
}
}
還有一種方式是重寫LinearLayoutManager,對其重寫 onMeasure 方法測量。
解決方案詳見:http://www.cnblogs.com/woaixingxing/p/6098726.html
但是,現在又出現了另一個問題,筆者使用上述兩種方式,不管是直接禁止RecyclerView不可滑動,重寫LinearLayoutManager,還是直接攔截滑動事件不分發給RecyclerView,Vivio x5plus 5.0手機確實不會卡頓,但是 Redmi Note4 6.0上,RecyclerView會出現顯示不全的情況。
針對這種情形,使用網上的方法一種是使用 RelativeLayout 包裹 RecyclerView 并設置屬性:<code>android:descendantFocusability="blocksDescendants"</code>
代碼如下:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/menuRv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_16"
android:layout_marginRight="@dimen/margin_16"/>
</RelativeLayout>
這種方式網上的說法是主要針對 6.0手機的解決方式,在 Redmi Note4 6.0手機上確實可以顯示完全,Vivio 5.0手機不會出現這種情況,僅需處理卡頓問題。
Note: <code>android:descendantFocusability="blocksDescendants"</code>,該屬>性是當一個view 獲取焦點時,定義 ViewGroup 和其子控件直接的關系,常用來>解決父控件的焦點或者點擊事件被子空間獲取。
屬性的值有三種:
- beforeDescendants: ViewGroup會優先其子控件獲取焦點
- afterDescendants: ViewGroup只有當其子控件不需要獲取焦點時才獲取焦點
- blocksDescendants: ViewGroup會覆蓋子類控件而直接獲得焦點
方式三:使用 NestedScrollView 嵌套 RecyclerView
這種方式較之 ScrollView 嵌套 RecyclerView 更簡單,不用復寫很多。布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="---HeaderView---"
android:focusable="true"
android:focusableInTouchMode="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
NestedScrollView 嵌套 RecyclerView也會出現 滑動卡頓問題,這是只需要禁止 RecyclerView 的滑動即可,通過在 xml 中給 RecyclerView 添加 android:nestedScrollingEnabled="false" 或者 直接設置 recyclerView.setNestedScrollingEnabled(false); 即可解決。
最后,還要說一下,不管是 ScrollView 還是 NestedScrollView 嵌套 RecyclerView 都會存在這樣的問題:
- 初次進入頁面,RecyclerView會直接顯示在頁面頂部,而不是 Header 部分,這是因為這種嵌套頁面焦點被 RecyclerView 獲取。解決方式:給 Header 部分View 添加屬性:
android:focusable="true"
android:focusableInTouchMode="true"
- 兩種方式嵌套,RecyclerView 會失去復用性,在數據量很大時,會存在內存消耗問題。解決方式之一:若Header部分是簡單布局,可將其作為 RecyclerView 的頭部部分,這種方式很常見。若Header部分比較復雜,如何保證 RecyclerView 的復用性,待嘗試。