一、页面初始化配置
private val mAdapter by lazy {
MainViewPage2Adapter(this).apply {
setFragmentListener {
//子fragment回调数据给act或fragment
}
}
}
mViewPager2 = findViewById(R.id.mViewPager2)
//1、关闭预加载方法-->
//方法一:是否支持预取 isItemPrefetchEnabled
(mViewPager2?.getChildAt(0) as RecyclerView).layoutManager?.apply {
isItemPrefetchEnabled = false
}
//方法二:想要限制页面预加载
mViewPager2.offscreenPageLimit=
ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
//2、设置数据 子条目bean 重写hashCode和equals
mAdapter.setNewData(fragmentList)
mViewPager2?.adapter = mAdapter
//3、tab vp 绑定 false即可去除切换动画
tabLayoutMediator = TabLayoutMediator(
tabLayout!!, mViewPager2!!, true, false
) { tab, position ->
tab.customView = getTabView(position)
}
tabLayoutMediator?.attach()
//4、是否开启离屏渲染,setup
mViewPager2?.offscreenPageLimit = mAdapter.itemCount
//5、注册your mPageChangeCallback
viewPager2.registerOnPageChangeCallback(mPageChangeCallback);
//6、切换到默认选择项,下一帧之后才有效
viewPager2.post(() -> viewPager2.setCurrentItem(currSelectPos, false));
二、ViewPager2获取指定页面的fragment 比如首页
(supportFragmentManager.findFragmentByTag(mAdapter.fragmentTag(0)) as? HomeRecyclerListFragment)?.xxx
动态diff更新
diff New List
val array = ArrayList<TabEntity>()
array.add(TabEntity("666", 0))
array.add(TabEntity("888", 1))
array.add(TabEntity("999", 2))
mAdapter.mergeList(array) {//call
showToast("success")
}
add tabEntity
mAdapter.addFragment(tabEntity,pos) {
//回调
tv_add_text?.text = "add success"
}
remove tabEntity
mAdapter.removeFragment(tabEntity) {
tv_add_text?.text = "del success"
}
子类继承Use BaseDiffPagerAdapter
class MainViewPage2Adapter(fragmentActivity: FragmentActivity) :
BaseDiffPager2Adapter<TabEntity,Boolean>(fragmentActivity) {
override fun genDiffItemCall(): DiffUtil.ItemCallback<TabEntity> {
return diffCallBack
}
override fun createFragment(position: Int): Fragment {
//listener为回调
return FragmentFactory.genFragment(position,listener)
}
companion object {
val diffCallBack = object : DiffUtil.ItemCallback<TabEntity>() {
override fun areItemsTheSame(oldItem: TabEntity, newItem: TabEntity): Boolean {
return oldItem.hashCode() == newItem.hashCode()
}
override fun areContentsTheSame(oldItem: TabEntity, newItem: TabEntity): Boolean {
return oldItem == newItem
}
}
}
}
定义接口能力
interface IViewPagerBasic<T,L> {
fun addFragment(addFragmentTab: T, insertPos: Int = -1, commitCallback: Runnable? = null) {
}
fun removeFragment(removeFragmentTab: T, commitCallback: Runnable? = null) {
}
fun removeFragment(pos: Int, commitCallback: Runnable? = null) {
}
fun genDiffItemCall(): DiffUtil.ItemCallback<T>
fun fragmentTag(pos: Int):String
fun setFragmentListener(l: ((L)->Unit)?=null){
}
}
基类BaseDiffPagerAdapter
/**
* 名称:支持 add remove 的viewpager2 adapter,支持异步diff
*/
abstract class BaseDiffPager2Adapter<T, L> : FragmentStateAdapter, IViewPagerBasic<T, L> {
private var mDiffer: AsyncListDiffer<T>? = null
protected var listener: ((L) -> Unit)? = null
constructor(fragment: Fragment) : super(fragment)
constructor(fragmentActivity: FragmentActivity) : super(fragmentActivity)
init {
mDiffer = AsyncListDiffer(this.getAdapter(), this.genDiffItemCall())
}
override fun setFragmentListener(l: ((L) -> Unit)?) {
this.listener = l
}
override fun containsItem(itemId: Long): Boolean {
mDiffer?.currentList?.forEach {
if (it.hashCode().toLong() == itemId) {
return true
}
}
return false
}
override fun addFragment(addFragmentTab: T, insertPos: Int, commitCallback: Runnable?) {
val arrayList = getMutablePageList()
if (!arrayList.contains(addFragmentTab)) {
val inPos = if (insertPos != -1 && insertPos <= itemCount) insertPos else itemCount
arrayList.add(inPos, addFragmentTab)
mergeList(arrayList, commitCallback)
}
}
override fun removeFragment(removeFragmentTab: T, commitCallback: Runnable?) {
removeFragmentTab?.let {
val arrayList = getMutablePageList()
val index = arrayList.indexOf(it)
if (index != -1) {
arrayList.remove(it)
mergeList(arrayList, commitCallback)
}
}
}
override fun removeFragment(pos: Int, commitCallback: Runnable?) {
val arrayList = getMutablePageList()
if (pos < arrayList.size) {
removeFragment(arrayList[pos], commitCallback)
}
}
override fun getItemCount(): Int {
return mDiffer?.currentList?.size ?: 0
}
fun setNewData(list: ArrayList<T>) {
mergeList(list)
}
fun mergeList(mNewList: ArrayList<T>, commitCallback: Runnable? = null) {
val arrayList = getMutablePageList()
if (arrayList.containsAll(mNewList) && arrayList.size == mNewList.size) return
mDiffer?.submitList(mNewList, commitCallback)
}
override fun getItemId(position: Int): Long {
if ((mDiffer?.currentList?.size ?: 0) < position) {
return 0L
}
return mDiffer!!.currentList[position].hashCode().toLong()
}
private fun getMutablePageList(): ArrayList<T> {
return ArrayList(mDiffer?.currentList ?: ArrayList())
}
private fun getAdapter(): BaseDiffPager2Adapter<T, L> {
return this
}
override fun fragmentTag(pos: Int): String {
return "f" + getItemId(pos)
}
}
FragmentFactory
object FragmentFactory {
//pos 回调
fun genFragment(index: Int,call: ((Boolean)->Unit)?=null): Fragment {
return when (index) {
0 -> {
HomeRecyclerListFragment.newInstance("1").apply {
setOnScrollListener(call)
}
}
1 -> { your fragment }
else -> {
TestListFragment.newInstance("99")
}
}
}
}