Kotlin中SwipeRefreshLayout結合RecyclerView下拉刷新上拉加載

轉載請標明出處:http://blog.csdn.net/donkor_/article/details/78820880

前言
最近做了一個純Kotlin開發的Android開源軟件,“DeepNight-in-kotlin,陪你度過每一個深夜”,抓取豆瓣美女的時候用到了SwipeRefreshLayout + RecyclerView 實現 上拉刷新 和 下拉刷新。功能完善好,代碼簡單貼一下,方便日後查看和使用。
下面看下效果圖。

實現的過程比較簡單,這裏直接貼代碼,註釋也寫的很清楚

基本配置
在Project的 build.gradle 中的dependencies添加:

implementation 'com.android.support:design:26.1.0'

主佈局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/mSwipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/mRvCommonList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

主佈局Kotlin類MainActivity

package com.donkor.demo.swiperefreshlayoutrecyclerview

import android.graphics.Color
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
    override fun onRefresh() {
        //關閉下拉刷新進度條
        mSwipeRefresh.isRefreshing = false
        addData()
    }

    private var mList: ArrayList<String>? = null
    var mCommonAdapter: CommonAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //下拉刷新顏色
        mSwipeRefresh.setColorSchemeColors(Color.rgb(47, 223, 189))
        mSwipeRefresh.setOnRefreshListener(this)
        //設置佈局管理器  layout可以設置方向
        mRvCommonList.layoutManager = LinearLayoutManager(this)
//        mRvCommonList.layoutManager = GridLayoutManager(context, 2)
        mList = ArrayList()
        mRvCommonList.setOnScrollListener(object : RecyclerView.OnScrollListener() {
            var lastVisibleItem: Int? = 0
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem!! + 1 == mCommonAdapter?.itemCount) {
                    addData()
                }
            }

            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val layoutManager = recyclerView?.layoutManager as LinearLayoutManager
                //最後一個可見的ITEM
                lastVisibleItem = layoutManager.findLastVisibleItemPosition()
            }
        })
        mCommonAdapter = CommonAdapter(this, mList)
        mRvCommonList.adapter = mCommonAdapter
    }

    //添加數據
    private fun addData() {
        mList!!.add("1111")
        mList!!.add("aaaa")
        mList!!.add("--------")

        mRvCommonList.adapter.notifyDataSetChanged()
    }
}

CommomAdapter自定義Item佈局

package com.donkor.demo.swiperefreshlayoutrecyclerview

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

/**
 * Created by donkor on 2017/12/16.
 */
class CommonAdapter(context: Context?, list: ArrayList<String>?) : RecyclerView.Adapter<CommonAdapter.CommonHolder>() {
    var mContext: Context? = null
    private var mList: ArrayList<String>? = null
    private var mInflater: LayoutInflater? = null

    init {
        mContext = context
        mList = list
        mInflater = LayoutInflater.from(context)
    }

    override fun getItemCount(): Int {
        return mList?.size ?: 0
    }

    override fun onBindViewHolder(holder: CommonHolder?, position: Int) {
        val title: String? = mList?.get(position)

        holder?.tvTitle?.text = title
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CommonHolder{
        return CommonHolder(mInflater?.inflate(R.layout.item_common,parent,false))
    }

    class CommonHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var tvTitle: TextView = itemView?.findViewById<TextView>(R.id.tv_title) as TextView
    }
}

item_commom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:paddingTop="10dp"
        android:src="@mipmap/ic_launcher"
        android:textColor="#000"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:text="asdqwe"
        android:textSize="12sp" />
</LinearLayout>

DeepNight-in-kotlin項目使用地址:https://github.com/ChenYXin/DeepNight-in-kotlin
Demo_CSDN 下載地址 :http://download.csdn.net/download/donkor_/10161572


About me
Email :[email protected]
Android開發交流QQ羣 : 537891203
Android開發交流羣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章