1.主页面代码
package com.cmmboy.module_business.kotlin
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter.RequestLoadMoreListener
import com.cmmboy.lib_basic.activity.BaseActivity
import com.cmmboy.lib_basic.utils.ToastUtils
import com.cmmboy.lib_basic.view.CustomLoadMoreView
import com.cmmboy.module_business.NewsViewModel
import com.cmmboy.module_business.R
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.business_activity_main2.*
import java.util.*
@Route(path = "/business/BusinessMainActivity")
class KNewsActivity : BaseActivity(), RequestLoadMoreListener {
private val newsViewModel by lazy { AndroidViewModelFactory(application).create(NewsViewModel::class.java) }
// private val viewModel by lazy { ViewModelProvider(this, InjectorUtil.getDailyViewModelFactory()).get(NewsViewModel::class.java) }
private lateinit var mAdapter: KNewsAdapter
// 当前页码, 从1开始
var pageNum = 1
var pageSize = 5
private val rowsBeanList = ArrayList<KNewsBean.Row>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.business_activity_main2)
initView()
}
private fun initView() {
refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
override fun onLoadMore(refreshLayout: RefreshLayout) {
pageNum++
getNet(false)
}
override fun onRefresh(refreshLayout: RefreshLayout) {
pageNum = 1
getNet(true)
}
})
refreshLayout.autoRefresh()
mAdapter = KNewsAdapter()
recyclerview.adapter = mAdapter
recyclerview.setLayoutManager(LinearLayoutManager(this))
mAdapter.setLoadMoreView(CustomLoadMoreView())
//设置滑动到底部时进行更多加载的回调
//设置滑动到底部时进行更多加载的回调
mAdapter.setOnLoadMoreListener(this, recyclerview)
//条目点击事件
//条目点击事件
mAdapter.onItemClickListener =
BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
ToastUtils.showShort(
rowsBeanList[position].newsTitle
)
}
}
override fun onLoadMoreRequested() {
pageNum++
getNet(false)
}
private fun getNet(isRefresh: Boolean) {
val newsTitle = ""
newsViewModel.getNews2(pageNum, pageSize, newsTitle).observe(this,
androidx.lifecycle.Observer<KNewsBean> { newsBeans ->
if (newsBeans.code == 200) {
val data = newsBeans.rows
rowsBeanList.addAll(data)
if (newsBeans.rows.isNotEmpty()) {
if (isRefresh) {
mAdapter.setNewData(data)
} else {
mAdapter.addData(data)
}
}
if (!newsBeans.hasNext) {
//下拉没数据了
// refreshLayout.setNoMoreData(true)
refreshLayout.setEnableLoadMore(false)
mAdapter.loadMoreEnd()
} else {
//继续下拉
refreshLayout.setNoMoreData(false)
mAdapter.loadMoreComplete()
}
if (null != refreshLayout) {
refreshLayout.finishRefresh()
refreshLayout.finishLoadMore()
}
}
})
}
}
2.adapter代码
package com.cmmboy.module_business.kotlin
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.cmmboy.module_business.R
class KNewsAdapter :
BaseQuickAdapter<KNewsBean.Row, BaseViewHolder>(R.layout.business_news_item) {
override fun convert(helper: BaseViewHolder, item: KNewsBean.Row?) {
val icon =
helper.getView<ImageView>(R.id.imageCover)
Glide.with(mContext)
.load("http://172.170.88.128:8081" + item!!.newsBanner)
.into(icon)
helper.setText(R.id.title, item.newsTitle)
}
}
3.布局
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.GoodThingsFragment">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>