RecyclerView实现上拉加载更多

RecyclerView这个控件是我们平常很常用的,相对于ListView和GridView来说也更加的灵活,废话不多说。

实现思路:
1.为Adapter添加脚布局
2.为RecyclerView添加滑动监听 - OnScrollListener

ok,就这么简单,让我们一步一步实现它。实现语言:Kotlin

首先我们先重写RecyclerView.Adapter,为我们的Adapter添加脚布局:

open class RecyclerAdapter : RecyclerView.Adapter {

    private val NORMAL = 0
    private val FOOT = 2
    private var mContext: Context
    private var mData = arrayListOf()
    var layout = R.layout.foot
    var layout2 = R.layout.foot2

    constructor(context: Context) {
        mContext = context
        for (i in 0..10) {
            mData.add("$i")
        }
    }

    fun addData(): Unit {
        for (i in 0..10) {
            mData.add("$i")
        }
//        setlayout(layout2)
        notifyItemChanged(itemCount - 1)
    }

    fun getlayout(): Int {
        return layout
    }

    fun setlayout(layout: Int): Unit {
        this.layout = layout
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        if (holder is RecyclerHold){
            holder.mTextView.text = mData[position]
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
       if (viewType == FOOT) {
            val recyclerHolder = FootHold(LayoutInflater.from(mContext).inflate(getlayout(), parent, false))
            return recyclerHolder
        } else {
            val recyclerHolder = RecyclerHold(LayoutInflater.from(mContext).inflate(R.layout.item, parent, false))
            return recyclerHolder
        }
    }

    override fun getItemCount(): Int {
        return mData.size + 1
    }

    override fun getItemViewType(position: Int): Int {
        if (position + 1 == itemCount) return FOOT
        return NORMAL
    }
}

然后创建两个RecyclerView.ViewHolder,一个用于普通的item,另一个用于脚布局。

class RecyclerHold : RecyclerView.ViewHolder{

    var mTextView: TextView

    constructor(itemView: View) : super(itemView){
        mTextView = itemView.findViewById(R.id.item) as TextView
    }
    
}

class FootHold(itemView: View) : RecyclerView.ViewHolder(itemView)

现在适配器完事了,我们来监听RecyclerView的滑动事件,如果RecyclerView滑动到了底部,我们就模拟添加更多的数据。

class MainActivity : AppCompatActivity() {

    lateinit var mRecyclerView: RecyclerView
    var isOk = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mRecyclerView = findViewById(R.id.recycler_view) as RecyclerView
        var linearLayoutManager: LinearLayoutManager = LinearLayoutManager(this)
        mRecyclerView.layoutManager = linearLayoutManager
        var adapter = RecyclerAdapter(this)
        mRecyclerView.adapter = adapter
        mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                var last = linearLayoutManager.findLastVisibleItemPosition()
                var sum = adapter.itemCount
                Log.d("!", "$newState")
                if(newState == RecyclerView.SCROLL_STATE_IDLE
                        && last + 1 == sum){
                    if (isOk){
                        isOk = false
                        Thread(Runnable {
                            Thread.sleep(1000)
                            runOnUiThread {
                                adapter.addData()
                                isOk = true
                            }
                        }).start()
                    }
                }

            }

            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

            }
        })

    }
}

onScrollStateChanged的newState有三个值:

 /**
     * The RecyclerView is not currently scrolling.
     * 手指离开屏幕
     */
    public static final int SCROLL_STATE_IDLE = 0;

    /**
     * The RecyclerView is currently being dragged by outside input such as user touch input.
     * 手指触摸屏幕
     */
    public static final int SCROLL_STATE_DRAGGING = 1;

    /**
     * The RecyclerView is currently animating to a final position while not under
     * outside control.
     * 手指加速滑动并放开,此时滑动状态伴随SCROLL_STATE_IDLE
     */
    public static final int SCROLL_STATE_SETTLING = 2;

我使用的是RecyclerView.SCROLL_STATE_IDLE,也就是当用户手指移开,开始执行事件。

收工!

笔者能力有限,不足之处欢迎指出。

你可能感兴趣的:(RecyclerView实现上拉加载更多)