ScrollView嵌套RecyclerView滑動沖突解決

最近發現自己負責的項目中,有使用 ScrollView 嵌套 RecyclerView 的地方,但是沒有做任何針對滑動沖突的處理,于是就想看下為什么沒有做這個處理,便進行了如下測試,發現了幾個奇怪的問題。

測試場景:頁面內容包括類似 HeaderView 的部分 + RecyclerView列表部分,布局是垂直方向,此處列表之上的布局內容并不是以 header add到RecyclerView上的。
測試結果:

  1. 在部分手機上,如果列表內容過少,只會造成很小程度的滑動,這種滑動沖突是沒法察覺到的,很容易忽略滑動沖突;
  2. 在部分手機上,列表內容過多時,滑動 RecyclerView 部分,會很卡頓,滑動 僅ScrollView 部分,很順暢,筆者試過Vivio x5plus 5.0系統會出現這樣的情況;
  3. 在部分手機上,不管列表內容多少,當滑動時,只有 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 都會存在這樣的問題:

  1. 初次進入頁面,RecyclerView會直接顯示在頁面頂部,而不是 Header 部分,這是因為這種嵌套頁面焦點被 RecyclerView 獲取。解決方式:給 Header 部分View 添加屬性:
android:focusable="true"
android:focusableInTouchMode="true"
  1. 兩種方式嵌套,RecyclerView 會失去復用性,在數據量很大時,會存在內存消耗問題。解決方式之一:若Header部分是簡單布局,可將其作為 RecyclerView 的頭部部分,這種方式很常見。若Header部分比較復雜,如何保證 RecyclerView 的復用性,待嘗試。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,333評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,491評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,263評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,946評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,708評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,186評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,255評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,409評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,939評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,774評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,976評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,518評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,209評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,641評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,872評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,650評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,958評論 2 373

推薦閱讀更多精彩內容