您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關使用ViewPager2時出現滑動沖突如何解決,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
MainActivity.java
package com.example.banner import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.viewpager2.widget.ViewPager2 class 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.banner import android.graphics.Color import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView class 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.banner import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import androidx.viewpager2.widget.ViewPager2 class 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.banner import android.content.Context import android.util.AttributeSet import android.view.MotionEvent import android.widget.RelativeLayout import androidx.viewpager2.widget.ViewPager2 class 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時出現滑動沖突如何解決有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。