Android ScrollView 嵌套RecyclerView滑動卡頓問題,并支持加載更多

ScrollView 和RecyclerView都是滑動組件,因此存在滑動事件沖突問題,解決思路就是在事件分發(fā)函數中將其中下層View的滑動事件做攔截處理

布局示例代碼如下:

    <com.xxx.xxx.widget.ScrollInterceptScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">
         <View
           android:id="@+id/divider2"
            android:layout_width="match_parent"
             android:layout_height="match_parent"
            android:background="#BBBBBB"
             app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:descendantFocusability="blocksDescendants"
                app:layout_constraintTop_toBottomOf="@+id/linearLayout3">


                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_menu"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="24dp"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


            </RelativeLayout>
         </View>
    </com.xxx.xxx.widget.ScrollInterceptScrollView>

注意:在布局中包裹一層RelativeLayout,并且加上這個屬性
android:descendantFocusability="blocksDescendants"
blocksDescendants這個屬性的官方解釋是viewgroup會覆蓋子類控件而直接獲得焦點

繼承ScrollView類自定義ScrollView

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

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);
    }

}
ScrollView滑動加載更多

判斷的思路就是當前滑動的y坐標是否大于等于當前ScrollView 的高度

//這個標志位是為了方式加載更多重復執(zhí)行
    private var isLoadMore = true
  //頁碼
    private var page=0

        //滑動處理,實現滑動監(jiān)聽方法
        scrollView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            //加載更多
            if (scrollY >= scrollView.getChildAt(0)
                    .measuredHeight - scrollView.measuredHeight
            ) {
                if (isLoadMore) {
                    Log.i("allyn", "加載更多")
                    isLoadMore = false;
                    page++
                    getData()
                }
            }
        }

講解完了,希望對遇到問題的朋友有幫助!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容