ScrollView 和RecyclerView都是滑動組件具则,因此存在滑動事件沖突問題即纲,解決思路就是在事件分發(fā)函數(shù)中將其中下層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
//滑動處理低斋,實現(xiàn)滑動監(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()
}
}
}
講解完了,希望對遇到問題的朋友有幫助7朔病膊畴!