前言:
最近做了一个纯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 下载地址 :