RecyclerView使用BaseProviderMultiAdapter实现无限循环

主要使用BaseProviderMultiAdapter,BaseItemProvider实现无限循环,并且能点击button、item、左右无限循环滑动。

Loop2Fragment类
package com.example.androidkotlindemo2.recyclerview.demo5

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import com.example.androidkotlindemo2.R
import com.example.androidkotlindemo2.bean.Loop2Bean
import com.example.androidkotlindemo2.bean.Test1Bean
import com.example.androidkotlindemo2.databinding.RecyclerViewLoop2MainBinding
import com.example.androidkotlindemo2.recyclerview.provider.Loop2BaseMultiProviderAdapter
import com.example.androidkotlindemo2.recyclerview.provider.Loop2Common
import com.example.androidkotlindemo2.utils.LogUtils

/**
 * Author : wn
 * Email : maoning20080809@163.com
 * Date : 2024/6/1 10:43
 * Description : 使用Loop2BaseMultiProviderAdapter实现无限循环
 */
class Loop2Fragment : Fragment() {

    private lateinit var binding : RecyclerViewLoop2MainBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = RecyclerViewLoop2MainBinding.inflate(inflater, container, false)
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        var adapter = Loop2BaseMultiProviderAdapter()
        var data = getData()
        adapter.data = data
        var linearLayoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        var linearSnapHelper = LinearSnapHelper()
        binding.rvLoop2RecyclerView.let {
            it.adapter = adapter
            it.layoutManager = linearLayoutManager
            linearSnapHelper.attachToRecyclerView(it)
            //跳转到中间
            //it.scrollToPosition(Int.MAX_VALUE/2)
            //跳转到第1个
            it.scrollToPosition((Int.MAX_VALUE - data.size) / 2)
        }

        adapter.setOnItemClickListener { adapter, view, position ->
            LogUtils.e("Loop2Fragment", "1setOnItemClickListener 点击item")
        }

        adapter.setOnItemChildClickListener { adapter, view, position ->
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王宁-Android

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值