Commit 1b63ddae authored by wanglei's avatar wanglei

...

parent 5a367956
......@@ -42,33 +42,32 @@ class NewsAdapter(
override fun onBindViewHolder(holder: NewsViewHolder, @SuppressLint("RecyclerView") position: Int) {
val binding = ItemNewsBinding.bind(holder.itemView)
val bean = beanList[holder.absoluteAdapterPosition]
val bean = beanList[holder.bindingAdapterPosition]
val context = holder.itemView.context
runCatching {
Glide.with(context).load(bean.orgImgPath).listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>,
isFirstResource: Boolean
): Boolean {
beanList.remove(bean)
notifyItemRemoved(position)
return true
}
Glide.with(context).load(bean.orgImgPath).listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>,
isFirstResource: Boolean
): Boolean {
LogEx.logDebug(TAG, "onLoadFailed $position ${holder.bindingAdapterPosition}")
beanList.remove(bean)
notifyItemRemoved(position)
return true
}
override fun onResourceReady(
resource: Drawable,
model: Any,
target: Target<Drawable>?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
return false
}
override fun onResourceReady(
resource: Drawable,
model: Any,
target: Target<Drawable>?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
return false
}
}).centerCrop().into(binding.ivMedia)
}
}).centerCrop().into(binding.ivMedia)
binding.tvTittle.text = bean.headline
runCatching {
Glide.with(context).load(bean.mediaIcon).into(binding.ivIcon)
......@@ -77,11 +76,19 @@ class NewsAdapter(
binding.tvPublishTime.text = bean.publishTime.toFormatTime()
}
fun loadMore(categoryId: Int = 0, callBack: (() -> Unit)? = null) {
fun loadMore(
categoryId: Int = 0,
errorCallBack: (() -> Unit)? = null,
callBack: (() -> Unit)? = null
) {
val position = beanList.size
val last = if (beanList.isEmpty()) null else beanList.last()
LogEx.logDebug(TAG, "newsId=${last?.newsId}")
NewsUtils.requestNews(categoryId, last?.newsId) {
NewsUtils.requestNews(categoryId, last?.newsId,
errorCallBack = {
errorCallBack?.invoke()
})
{
beanList.addAll(it)
lifecycleScope.launch(Dispatchers.Main) {
this@NewsAdapter.notifyItemInserted(position)
......
......@@ -64,23 +64,32 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
binding.rvNews.addOnScrollListener(object : OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val lastVisibleItemPosition = (recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
if (!isLoadingMore && lastVisibleItemPosition == newsAdapter.itemCount - 5) {
val lastVisibleItemPosition =
(recyclerView.layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition()
LogEx.logDebug(
TAG,
"isLoadingMore=$isLoadingMore lastVisibleItemPosition=$lastVisibleItemPosition ${newsAdapter.itemCount} "
)
if (!isLoadingMore && lastVisibleItemPosition == newsAdapter.itemCount - 1) {
isLoadingMore = true
// 触发加载更多数据
newsAdapter.loadMore {
LogEx.logDebug(TAG, "loadMore BACK")
isLoadingMore = false
}
newsAdapter.loadMore(
errorCallBack = {},
callBack = {
LogEx.logDebug(TAG, "loadMore BACK")
isLoadingMore = false
})
}
}
})
isLoadingMore = true
newsAdapter.loadMore {
LogEx.logDebug(TAG, "loadMore BACK")
isLoadingMore = false
}
newsAdapter.loadMore(
errorCallBack = {},
callBack = {
LogEx.logDebug(TAG, "loadMore BACK")
isLoadingMore = false
})
}
override fun onResume() {
......
......@@ -36,6 +36,7 @@ object NewsUtils {
fun requestNews(
categoryId: Int = 0,
pageNumber: Long? = null,
errorCallBack:()->Unit,
beanCallBack: (beanList: List<NewsBean>) -> Unit
) {
val pkg = ConfigHelper.packageName
......@@ -83,6 +84,7 @@ object NewsUtils {
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
LogEx.logDebug(TAG, "onFailure $e")
errorCallBack.invoke()
}
override fun onResponse(call: Call, response: Response) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment