降低ViewPager2灵敏度

object ViewPagerUtil {
    /**
     * 降级ViewPager2灵敏度
     */
    fun ViewPager2.desensitization(){
        //动态设置ViewPager2 灵敏度
        try {
            val recyclerViewField: Field = ViewPager2::class.java.getDeclaredField("mRecyclerView")
            recyclerViewField.isAccessible = true
            val recyclerView = recyclerViewField.get(this) as RecyclerView
            val touchSlopField: Field = RecyclerView::class.java.getDeclaredField("mTouchSlop")
            touchSlopField.isAccessible = true
            val touchSlop = touchSlopField.get(recyclerView) as Int
            touchSlopField.set(recyclerView, touchSlop * 3) //6 is empirical value
        } catch (ignore: java.lang.Exception) {
        }
    }
}

你可能感兴趣的:(Android技术,java,android,开发语言)