今天就跟大家聊聊有关使用ViewPager2时出现滑动冲突如何解决,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
MainActivity.java
package com.example.bannerimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport androidx.viewpager2.widget.ViewPager2class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val viewPager2 = findViewById<ViewPager2>(R.id.viewpager2outside) val myAdapter = OutsideAdapter() viewPager2.adapter = myAdapter }}
InnerAdapter
package com.example.bannerimport android.graphics.Colorimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport android.widget.TextViewimport androidx.recyclerview.widget.RecyclerViewclass InnerAdapter : RecyclerView.Adapter<InnerAdapter.PagerViewHolder>() { private var mList: List<Int> = ArrayList() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerViewHolder { val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_page, parent, false) return PagerViewHolder(itemView) } override fun onBindViewHolder(holder: PagerViewHolder, position: Int) { holder.bindData(mList[position]) } fun setList(list: List<Int>) { mList = list } override fun getItemCount(): Int { return mList.size } // ViewHolder需要继承RecycleView.ViewHolder class PagerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val mTextView: TextView = itemView.findViewById(R.id.tv_text) private var colors = arrayOf("#41F1E5","#8D41F1","#FF99CC","#41F1E5") fun bindData(i: Int) { mTextView.text = i.toString() mTextView.setBackgroundColor(Color.parseColor(colors[i])) } }}
OutsideAdapter
package com.example.bannerimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.recyclerview.widget.RecyclerViewimport androidx.viewpager2.widget.ViewPager2class OutsideAdapter() : RecyclerView.Adapter<OutsideAdapter.PagerViewHolder>() { class PagerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val mPagerView: ViewPager2 = itemView.findViewById(R.id.viewpager2inner) fun bindData() { val data = listOf(0, 1, 2, 3) val myAdapter = InnerAdapter() myAdapter.setList(data) mPagerView.adapter = myAdapter } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerViewHolder { val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_outside, parent, false) return PagerViewHolder(itemView) } override fun onBindViewHolder(holder: PagerViewHolder, position: Int) { holder.bindData() } override fun getItemCount(): Int = 2}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/viewpager2outside" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
item_outside.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/viewpager2inner" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
item_page.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:id="@+id/tv_text" android:background="#0000ff" android:gravity="center" android:layout_centerHorizontal="true" android:layout_width="match_parent" android:layout_height="280dp" android:textColor="#ffffff" android:textSize="82sp" /></RelativeLayout>
如果解决了滑动冲突应该在TextView对应的区域滑动时应该能从0滑动到1,而导致图中效果的原因是因为外层的ViewPager2拦截了横向的滑动事件,因为ViewPager2是final不能继承,所以我们给它套一层父ViewGroup来解决滑动冲突问题,这里我是在外面的ViewPager2里面放了俩个ViewPager2 我想做到的是从第一个里面的Vp2(ViewPager2)0划到1再到2到3再到里面的第二个Vp2的0再到1再到2再到3,而不是直接从第一个里面的Vp2(ViewPager2)0滑倒里面的第二个Vp2的0。
下面上源码,只上有改动的代码,像MainActivity及activity_main.xml等没有改动就不再上了
ViewPager2Container
package com.example.bannerimport android.content.Contextimport android.util.AttributeSetimport android.view.MotionEventimport android.widget.RelativeLayoutimport androidx.viewpager2.widget.ViewPager2class ViewPager2Container @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : RelativeLayout(context, attrs, defStyleAttr) { private lateinit var mViewPager2: ViewPager2 override fun onFinishInflate() { super.onFinishInflate() for (i in 0 until childCount) { val childView = getChildAt(i) if (childView is ViewPager2) { mViewPager2 = childView break } } } override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { when (ev.action) { //不能让父View拦截事件否则父View会拦截接下来的一系列事件 MotionEvent.ACTION_DOWN -> { parent.requestDisallowInterceptTouchEvent(true) } MotionEvent.ACTION_MOVE -> { if(mViewPager2.currentItem!=mViewPager2.adapter!!.itemCount-1){ parent.requestDisallowInterceptTouchEvent(true) }else{ parent.requestDisallowInterceptTouchEvent(false) } } } return super.onInterceptTouchEvent(ev) }}
item_outside.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.banner.ViewPager2Container android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent"> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/viewpager2inner" android:layout_width="match_parent" android:layout_height="280dp" android:overScrollMode="never" /> </com.example.banner.ViewPager2Container></androidx.constraintlayout.widget.ConstraintLayout>
看完上述内容,你们对使用ViewPager2时出现滑动冲突如何解决有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。