gpt4 book ai didi

android - Kotlin RecyclerView 添加加载页脚

转载 作者:行者123 更新时间:2023-12-02 12:21:38 26 4
gpt4 key购买 nike

我已经使用此视频为我的 RecyclerView 实现了标题:https://www.youtube.com/watch?v=ksIpDnLiCMs

但我不知道如何将加载页脚与 ProgressBar 集成,我尝试自己实现它但它不起作用,当我向下时它应该加载更多但无论我的滚动条在哪里它都会继续加载位置,进度条不会出现。这是我的代码:

MainActivity.kt:

class MainActivity : AppCompatActivity() {

val numberList: MutableList<String> = ArrayList()

var page = 1
var isLoading = false
var limit = 10

lateinit var adapter: RV_Adapter
lateinit var layoutManager: LinearLayoutManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager

getPage()

recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

val visibleItemCount = layoutManager.childCount
val pastVisibleItem = layoutManager.findFirstCompletelyVisibleItemPosition()
val total = adapter.itemCount

if(!isLoading) {
if((visibleItemCount + pastVisibleItem) >= total) {
page++
getPage()
}
}

super.onScrolled(recyclerView, dx, dy)
}
})
}

fun getPage() {
isLoading = true
//progressBar.visibility = View.VISIBLE
val start : Int = (page-1)*limit
val end : Int = page * limit

for(i in start..end){
numberList.add("PodView " + i.toString())
}
Handler().postDelayed({
if(::adapter.isInitialized) {
adapter.notifyDataSetChanged()
} else {
adapter = RV_Adapter(this, numberList)
recyclerView.adapter = adapter
}
isLoading = false
//progressBar.visibility = View.GONE
}, 0)

}
}

RV_Adapter.kt

class RV_Adapter(private val context: Context, private val numberList: MutableList<String>) : RecyclerView.Adapter<BaseViewHolder<*>>() {

companion object {
private const val TYPE_HEADER = 0
private const val TYPE_ITEMS = 1
private const val TYPE_FOOTER = 2
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {

return when(viewType) {
TYPE_HEADER -> {
val view = LayoutInflater.from(context).inflate(R.layout.rv_header, parent, false)
HeaderViewHolder(view)
}

TYPE_ITEMS -> {
val view = LayoutInflater.from(context).inflate(R.layout.listview_item, parent, false)
ItemViewHolder(view)
}

TYPE_FOOTER -> {
val view = LayoutInflater.from(context).inflate(R.layout.rv_footer, parent, false)
FooterViewHolder(view)
}

else -> throw IllegalArgumentException("Invalid View Type")
}

}

override fun getItemCount(): Int {
return numberList.size + 1
}

override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
if(holder is ItemViewHolder){
holder.numberBarText.text = numberList[position]
}
if(holder is HeaderViewHolder){
holder.cardView.radius = 50f
}


}

override fun getItemViewType(position: Int): Int {
if(position == 0) {
return TYPE_HEADER
}

return TYPE_ITEMS
}

inner class HeaderViewHolder(itemView: View): BaseViewHolder<View>(itemView) {
val cardView : CardView = itemView.findViewById(R.id.profileImageCardView)

override fun bind(item: View) {}

}

inner class ItemViewHolder(itemView: View): BaseViewHolder<View>(itemView) {
val numberBarText = itemView.findViewById<TextView>(R.id.numberBarText)

override fun bind(item: View) {}

}
inner class FooterViewHolder(itemView: View): BaseViewHolder<View>(itemView) {

override fun bind(item: View) {}

}
}

UPDATE:

将我的 RecyclerView 与 BottomNavigationView 组合后,页脚和页眉出现故障。需要一个更可靠的解决方案,而不是像这样向数组列表添加字符串:numberList.add("VIEW_TYPE_FOOTER")

最佳答案

为了在您的适配器中创建一个页脚项目,您应该在您的代码中实现一些东西。

首先,您应该为页脚创建一个新的 itemViewType。您必须做的第二件事是创建一种方式,告诉您的 Adapter 这个 View 实际上是一个页脚。由于您将 ListString 一起使用,因此您应该在 List 的末尾添加如下内容:

numberList.add("VIEW_TYPE_FOOTER")

之后:

override fun getItemViewType(position: Int): Int {
val item = numberList[position]
if(position == 0) {
return TYPE_HEADER
} else if(item == "VIEW_TYPE_FOOTER") {
return TYPE_FOOTER
}
return TYPE_ITEMS
}

并且您应该检查 lastVisibleItem,而不是 firstVisible,然后获取 nextPage。请记住,在获取下一页后,您必须从 List 中删除最新的项目,这将成为您的页脚 View ,将下一页添加到 List 中,然后决定是否需要添加新页脚。

希望这能帮助您了解如何让它在您的情况下发挥作用。

关于android - Kotlin RecyclerView 添加加载页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59270339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com