初始代码提交
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package com.sw.inbound.view
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.MotionEvent
|
||||
import android.view.ViewConfiguration
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlin.math.abs
|
||||
|
||||
|
||||
class NestedRecyclerView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0
|
||||
) : RecyclerView(context, attrs, defStyleAttr) {
|
||||
|
||||
private var lastY = 0f
|
||||
private var isDragging = false
|
||||
|
||||
private var touchSlop = ViewConfiguration.get(context).scaledTouchSlop
|
||||
|
||||
override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
|
||||
when (e.action) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
lastY = e.y
|
||||
parent.requestDisallowInterceptTouchEvent(true)
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
val dy = e.y - lastY
|
||||
if (!canScrollVertically(-1) && dy > 0) {
|
||||
parent.requestDisallowInterceptTouchEvent(false)
|
||||
return false
|
||||
} else if (!canScrollVertically(1) && dy < 0) {
|
||||
parent.requestDisallowInterceptTouchEvent(false)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.onInterceptTouchEvent(e)
|
||||
}
|
||||
|
||||
override fun onTouchEvent(e: MotionEvent): Boolean {
|
||||
when (e.action) {
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
if (!isDragging && abs(e.y - lastY) > touchSlop) {
|
||||
isDragging = true
|
||||
}
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
isDragging = false
|
||||
}
|
||||
}
|
||||
return super.onTouchEvent(e)
|
||||
}
|
||||
|
||||
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
|
||||
when (ev.action) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
stopNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
|
||||
}
|
||||
}
|
||||
return super.dispatchTouchEvent(ev)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.sw.inbound.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
public class NonScrollRecyclerView extends RecyclerView {
|
||||
|
||||
public NonScrollRecyclerView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public NonScrollRecyclerView(Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean dispatchTouchEvent(MotionEvent ev) {
|
||||
// 请求父View不拦截事件,确保子View能接收到事件
|
||||
getParent().requestDisallowInterceptTouchEvent(true);
|
||||
return super.dispatchTouchEvent(ev);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.sw.inbound.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.ViewConfiguration;
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
public class ParentRecyclerView extends RecyclerView {
|
||||
private float downX, downY;
|
||||
private boolean isIntercept = false;
|
||||
|
||||
public ParentRecyclerView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public ParentRecyclerView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
||||
switch (ev.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
downX = ev.getX();
|
||||
downY = ev.getY();
|
||||
isIntercept = false;
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
float deltaX = Math.abs(ev.getX() - downX);
|
||||
float deltaY = Math.abs(ev.getY() - downY);
|
||||
|
||||
// 判断滑动方向,如果是垂直滑动则让父RecyclerView处理
|
||||
if (deltaY > deltaX && deltaY > getTouchSlop()) {
|
||||
isIntercept = true;
|
||||
} else {
|
||||
isIntercept = false; // 水平滑动不拦截
|
||||
}
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_UP:
|
||||
isIntercept = false;
|
||||
break;
|
||||
}
|
||||
|
||||
return isIntercept;
|
||||
}
|
||||
|
||||
private int getTouchSlop() {
|
||||
return ViewConfiguration.get(getContext()).getScaledTouchSlop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user