支持下拉刷新上拉加载的RecyclerView
2019-12-02 本文已影响0人
请叫我刘中二
前言
纵观多数App,下拉刷新和上拉加载更多是很常见的功能,但是谷歌官方只有一个SwipeRefreshLayout用来下拉刷新,上拉加载更多还要自己做。
集成
第一种:maven库引用
#在项目的build.gradle中加下以下maven地址
maven {
url 'https://dl.bintray.com/module-android/SimplePullView/'
}
#在app目录的build.gradlezhon加
compile 'com.jason.simple:PullRecyclerView:1.0.3'
使用
1.直接使用SRecyclerView进行布局(head_height是用于设置松开手后,下拉刷新的触发高度和展示高度)
```
<com.jason.recyclerview.SRecyclerView
android:id="@+id/rv_branch_bank"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:head_height="80dp">
</com.jason.recyclerview.SRecyclerView>
```
2.SRecyclerView的适配器需要继承SRecyclerAdapter,并实现onCreateHolder,onBindHolder,getCount(),getViewType4个方法
class BranchBankAdapter(var mContext: Context,var list: MutableList<String>): SRecyclerAdapter(mContext) {
override fun onCreateHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view=View.inflate(mContext,R.layout.item_bank_sup_layout,null)
view.layoutParams= ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
return BranchBankViewHolder(view)
}
override fun onBindHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
(viewHolder as BranchBankViewHolder).setData(position)
}
override fun getCount(): Int=list.size
override fun getViewType(position: Int): Int=0
class BranchBankViewHolder(var view:View):RecyclerView.ViewHolder(view){
fun setData(position: Int){
view.tv_first.text="交通银行上海六里河支行$position"
view.tv_second.text="支行联行号:736353684484"
}
}
}
3.SRecyclerListener用于监听SRecyclerView的下拉刷新和上拉加载的动作
SRecyclerView.setSRecyclerListener(object : SRecyclerListener {
override fun loadMore() {
handler.postDelayed({
for (i in 0 until 10){
list.add("你好世界")
}
rv_branch_bank.notifyDataSetChanged()
},1000)
}
override fun refresh() {
handler.postDelayed({
list.clear()
for (i in 0 until 50){
list.add("你好世界")
}
rv_branch_bank.complete()
mAdapter?.notifyDataSetChanged()
},5000)
}
})
4.支持自定义刷新头和加载尾布局
刷新头需要继承RefreshViewHolder,在构造方法中传入自己的布局,并重写notifyHeight
加载尾同上
例子可以参考库中的 DefaultRefreshViewHolder
使用:可以通过适配器设置刷新头和加载尾
mAdapter.setFootViewHolder("传入自定义的加载尾")
mAdapter.setHeadViewHolder("传入自定义的刷新头")
5.控制是否允许刷新或者加载
SRecyclerView.isCanLoadMore
SRecyclerView.isCanRefresh
6.设置layoutManager(默认LinerLayoutManager )
SRecyclerView.setLayoutManager
请务必通过此方法调用setLayoutManager
7.设置spanSizeLookup
SRecyclerView.setSpanSize
请务必通过此方法调用GridLayoutManager的设置spanSizeLookup
代码地址
(https://github.com/liujun123456/jason/tree/master/srecyclerview)