项目引入依赖
1、在bulid.gradle(Project:xxx)的文件中添加
allprojects {
repositories {
//jcenter镜像
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url "https://jitpack.io" }
}
}
2、在bulid.gradle(Module:xxx)的文件添加
dependencies {
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'
// 下拉刷新
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0'
}
使用案例
3.1 设置布局文件
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smart"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlEnableHeaderTranslationContent="false">
<com.scwang.smartrefresh.header.MaterialHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recy_kongjian"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
3.2 自定义适配器,继承BaseQuickAdapter
class TiXingAdapter : BaseQuickAdapter<QtypeVo, BaseViewHolder>(R.layout.item_tixing),LoadMoreModule {
override fun convert(holder: BaseViewHolder, item: QtypeVo) {
val tvName = holder.getView<TextView>(R.id.tv_content)
if (item.isSelected) {
tvName.background = MyApp.instance.resources.getDrawable(R.drawable.shape_tv_ckbg_bg)
tvName.setTextColor(ZJColor.white)
} else {
tvName.background = MyApp.instance.resources.getDrawable(R.drawable.shape_btn_bg_gray)
tvName.setTextColor(ZJColor.black_33)
}
tvName.text = item.qtypename
}
}
3.3 调用,如果xml中没有设置layoutManager,那可以声明的时候设置
tiXingAdapter = TiXingAdapter ()
tiXingAdapter?.data = kemuDatas
recy_kemu.layoutManager = GridLayoutManager(this, 3)
recy_kemu.adapter = tiXingAdapter
切记一定要设置布局管理器否则数据出不来
适配器点击事件
4.1 item点击事件
tiXingAdapter?.setOnItemClickListener { adapter, view, position ->
//item点击事件
}
4.2 item长按事件
tiXingAdapter?.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
return false;
}
});
4.3 item 子控件点击事件
一般用于RecyclerView嵌套RecyclerView或者滑动删除等其他的操作
tiXingAdapter?.addChildClickViewIds(R.id.wvItem, R.id.fCon)
tiXingAdapter?.setOnItemChildClickListener() { adapter, view, position ->
//点击事件
}
4.4 数据为空的显示的ui
tiXingAdapter?.setEmptyView(R.layout.empty_bg)
下拉刷洗上拉加载更多
smart.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
override fun onLoadMore(refreshLayout: RefreshLayout) {
loadmore()
}
override fun onRefresh(refreshLayout: RefreshLayout) {
refresh()
}
})
结束刷新
override fun endRefreshAndLoadMore() {
super.endRefreshAndLoadMore()
smart.finishRefresh()
smart.finishLoadMore()
}
在p层的时候记得要调用
mView.endRefreshAndLoadMore()
如果加载到最后一页没有数据了提示没有更多数据
smart.setNoMoreData(true)//没有更多数据
smart.setNoMoreData(false)//还有更多数据 默认的是false