(一) 超简单自定义上下刷新布局-LtRecyclerView v2.x版本(基本使用)
(二) LtRecyclerView v2.x (更多实用方法)
(三) LtRecyclerView v2.x (自定义刷新View)
6.自定义上拉加载的View
6.1.通过setUpLayoutId()方法传入一个id可以控制全局的上拉刷新View
//自定义配置全局的上拉加载View
LtRecyclerViewManager.INSTANCE.init(this)
.setUpLayoutId(R.layout.lt_up_loading);//此方法设置一个layout的id,设置为上拉加载的View
原理是通过显示和隐藏布局内的n个View来表示刷新与否
设置该方法的布局,刷新的布局为第一个,刷新完成的布局为第二个,其他的可以通过LtAdapter.setRefresh(Int)来设置索引(从0开始)
布局伪代码示例:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout >//根布局,可以是任意ViewGroup
<LinearLayout/>//索引0 默认是刷新时的view
<LinearLayout/>//索引1 默认是刷新完成后的view
//后面可以添加更多的View,通过setRefresh(Int索引)来控制显示与隐藏
</FrameLayout>
6.2.若要单独配置某一个适配器的上拉加载View
则在LtAdapter的构造里传入相应的View即可
6.3.如果不想要上拉加载,则在LtAdapter的构造里传null,默认使用全局的使用空参构造即可
7.自定义下拉刷新的View
7.1.内部有两个下拉刷新,一个是系统的SwipeRefreshLayout(已移除,如果需要可以自己自己继承SwipeRefreshLayout并实现BaseRefreshLayout接口)
和我预定义的MTextRefreshLayout
默认使用的是SwipeRefreshLayout:
可以设置为MTextRefreshLayout:
LtRecyclerViewManager.INSTANCE.init(this)
//java8
.setRefreshLayoutConstructorFunction(MTextRefreshLayout::new);
//kotlin使用方式
.setRefreshLayoutConstructorFunction(::MTextRefreshLayout)
//java7及以下使用方式
.setRefreshLayoutConstructorFunction(new Function3<Context, AttributeSet, Integer, BaseRefreshLayout>() {
@Override
public BaseRefreshLayout invoke(Context context, AttributeSet attributeSet, Integer defStyleAttr) {
return new NewWinkChatRefreshLayout(context, attributeSet, defStyleAttr);
}
});
也可以更改为rv不移动,只拉出刷新的View:
LtRecyclerViewManager.INSTANCE.init(this)
.setRvIsMove(false)//设置下拉刷新时rv是否跟着移动,只有在自定义时可用
.setRefreshLayoutConstructorFunction(MTextRefreshLayout::new);
7.2.也可以通过继承LtRefreshLayout来实现下拉刷新的View
示例:
public class MRefreshLayout extends LtRefreshLayout {//继承LtRefreshLayout并重写方法
public MRefreshLayout(Context context) {
super(context);
}
public MRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onStatus(int status) {
//当状态变更时调用,在此方法中更改刷新View的状态,一共有四个状态
public final static int STATE_REFRESH_DOWN = 0;//下拉中
public final static int STATE_REFRESH_RELEASE = 1;//松开刷新
public final static int STATE_REFRESHING = 2;//刷新中
public final static int STATE_REFRESH_FINISH = 3;//刷新完成
public final static int STATE_BACK = 4;//刷新结束,并且刷新View隐藏到了顶部
}
@Override
public void onProgress(float y) {
//当前y轴移动了多少,一般不需要这个方法,注意该方法会被频繁调用,不要在里面创建对象或耗时操作
}
@Override
protected View getRefreshView() {//返回下拉刷新的View
return null;
}
}
LtRecyclerViewManager.INSTANCE.init(this)
//.setRefreshThreshold(100)//设置松开刷新的位置(全局的阈值)
.setRefreshLayoutConstructorFunction(MRefreshLayout::new);//配置全局使用自定义的刷新View
上面就完成了自定义的下拉刷新View,下拉时移动等操作已经在内部实现了,只需要控制刷新View的展示即可
而且自定义的下拉刷新View可以直接在xml文件内使用
对Kotlin或KMP感兴趣的同学可以进Q群 101786950
如果这篇文章对您有帮助的话
可以扫码请我喝瓶饮料或咖啡(如果对什么比较感兴趣可以在备注里写出来)