Commit 1b63ddae authored by wanglei's avatar wanglei

...

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