[Modify]优化Viewpager2滑动太灵敏

This commit is contained in:
wushaocheng
2023-02-03 18:09:21 +08:00
parent 7fa3e781d1
commit ea94b27bd4
5 changed files with 80 additions and 10 deletions

View File

@@ -92,7 +92,7 @@
android:gravity="center_vertical" android:gravity="center_vertical"
android:orientation="horizontal"> android:orientation="horizontal">
<ImageView <androidx.appcompat.widget.AppCompatImageView
android:layout_width="48dp" android:layout_width="48dp"
android:layout_height="48dp" android:layout_height="48dp"
android:layout_marginStart="@dimen/dp_12" android:layout_marginStart="@dimen/dp_12"
@@ -122,7 +122,7 @@
android:visibility="gone" android:visibility="gone"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" /> app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<ImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivGameGuide" android:id="@+id/ivGameGuide"
android:src="@drawable/ic_game_guide" android:src="@drawable/ic_game_guide"
android:visibility="gone" android:visibility="gone"
@@ -142,7 +142,7 @@
android:textSize="@dimen/sp_16" android:textSize="@dimen/sp_16"
android:textStyle="bold" /> android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView <com.yizhuan.xchat_android_library.common.widget.VpRecyclerView
android:id="@+id/mRecyclerRoom" android:id="@+id/mRecyclerRoom"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView <com.yizhuan.xchat_android_library.common.widget.VpRecyclerView
android:id="@+id/recycler_view" android:id="@+id/recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"

View File

@@ -9,7 +9,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView <com.yizhuan.xchat_android_library.common.widget.VpRecyclerView
android:id="@+id/recycler_view" android:id="@+id/recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -28,7 +28,7 @@
android:paddingBottom="2dp" android:paddingBottom="2dp"
android:text="@string/layout_fragment_room_game_01" android:text="@string/layout_fragment_room_game_01"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="16dp" /> android:textSize="@dimen/sp_16" />
</FrameLayout> </FrameLayout>

View File

@@ -26,7 +26,7 @@
android:textColor="#b3333333" android:textColor="#b3333333"
android:textSize="14sp" /> android:textSize="14sp" />
<ImageView <androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="68dp" android:layout_marginTop="68dp"
@@ -97,13 +97,13 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="10dp" android:layout_marginEnd="10dp"
android:drawableRight="@drawable/arrow_right"
android:text="@string/layout_fragment_room_like_04" android:text="@string/layout_fragment_room_like_04"
android:textColor="#b3333333" android:textColor="#b3333333"
android:textSize="12sp" android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@id/tv_like" app:layout_constraintBottom_toBottomOf="@id/tv_like"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_like" /> app:layout_constraintTop_toTopOf="@id/tv_like"
app:drawableRightCompat="@drawable/arrow_right" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_like" android:id="@+id/rv_like"
@@ -138,7 +138,7 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView <com.yizhuan.xchat_android_library.common.widget.VpRecyclerView
android:id="@+id/rv_collect" android:id="@+id/rv_collect"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"

View File

@@ -0,0 +1,70 @@
package com.yizhuan.xchat_android_library.common.widget
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs
/**
* author : wushaocheng
* date : 2023/2/3
* desc : 滑动冲突。斜着往上滑的时候会滑动viewpage
*/
class VpRecyclerView(context: Context, attrs: AttributeSet?) : RecyclerView(context, attrs) {
private var mTouchSlop = 0
var startX = 0 //手指碰到屏幕时的 X坐标
var startY = 0 //手机碰到屏幕时的 Y坐标
init {
val vc = ViewConfiguration.get(context)
mTouchSlop = vc.scaledTouchSlop
}
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
when (ev?.action) {
MotionEvent.ACTION_DOWN -> {
//当手指按下时得到了XY
startX = ev.x.toInt()
startY = ev.y.toInt()
//
parent.requestDisallowInterceptTouchEvent(true)
}
MotionEvent.ACTION_MOVE -> {
//抬起手后得到的坐标,
val endX = ev.x.toInt()
val endY = ev.y.toInt()
//得到绝对值 。
val disX = abs(endX - startX)
val disY = abs(endY - startY)
//如果X轴 大于Y 轴,说明实在左右移动 为什么?
// 屏幕坐标XY从左上角开始。00
if (disX > disY) {
Log.e("ACTIONdisX > disY:", "$disX")
//这个地方判断了左右滑动的灵敏度只有当左右滑动距离110 此时父布局才有作用,不拦截。
if (disX > 110) { //结束的时候大于
//当滑动的距离大于100的时候才不拦截parent的事件 父控件才会有用。
parent.requestDisallowInterceptTouchEvent(false)
}
} else {
// 说明是上下滑动 //canScrollVertically 检查此视图是否可以按某个方向垂直滚动。 负数表示上下滚动。正数表示左右滚动
//return true如果视图可以按指定的方向滚动否则为false。
//既然是上下滑动,此时,父控件就不能有 事件 true停止
parent.requestDisallowInterceptTouchEvent(true)
}
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> parent.requestDisallowInterceptTouchEvent(false)
}
return super.dispatchTouchEvent(ev)
}
}