更改recyclerview的页脚itemView的视图文本 [英] Change the view text of footer itemView of recyclerview
问题描述
我有一个条件,我必须在文本中添加不同的viewType
Hi I have a condition where I have to add different viewType with text
show more
或loading...
位于回收站视图的底部
我已经成功地添加了带show more的页脚视图,并且还在此页脚项目视图上添加了侦听器以加载更多数据,这里一切正常.由于我将刷新刷新布局作为recyclerview的父级,因此我在加载时显示启用swiperefresh进度,并在加载完成时禁用swiperefresh进度.现在,我需要的是每当单击show more
视图类型时,该视图类型是附加到回收者视图的页脚,我希望使用loading...
更改文本,并且在完成加载后,应将文本更改为show more
再次.
I have successfully added the footer view with show more and also I have added the listener on this footer item view for loading more data, here everything works fine. Since I have swipe refresh layout as a parent for recyclerview I show enable swiperefresh progress on loading and disable swiperefresh progress while loading is complete. Now what I need is whenever I click on the show more
view type which is a footer attached to the recycler view I want the text to be changed with loading...
and when the loading is completed the text should be changed to show more
again.
这是我的适配器类
class MyTransactionAdapter(private val context: Context?,
private val transactionList: List<TransactionHistoryResponse>,
private val transactionListener: MyTransactionListener) : Filterable,
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var viewTypeList: Int = 0
private var viewTypeFooter: Int = 1
private var transactionListFiltered = transactionList
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == viewTypeList) {
val view: View = LayoutInflater.from(context).inflate(R.layout.row_my_transaction, parent, false)
ListViewHolder(view)
} else {
val view: View = LayoutInflater.from(context).inflate(R.layout.row_footer_so_more, parent, false)
FooterViewHolder(view)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val myTransaction = transactionListFiltered[position]
if (holder is ListViewHolder) {
holder.bind(myTransaction, transactionListener)
}
}
override fun getItemCount(): Int {
return transactionListFiltered.size
}
override fun getItemViewType(position: Int): Int {
return if (position == transactionList.lastIndex) {
viewTypeFooter
} else {
viewTypeList
}
}
inner class FooterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
init {
itemView.apply {
btnShowMore.setOnClickListener {
transactionListener.onLoadMoreClicked()
}
}
}
fun enableShowMore(enable: Boolean) {
if(enable){
itemView.btnShowMore.text = "show more"
}else{
itemView.btnShowMore.text = "loading..."
}
}
}
inner class ListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private lateinit var mTransactionStatus: TransactionStatus
fun bind(myTransaction: TransactionHistoryResponse, transactionListener: MyTransactionListener){
//logic to populate data on views
}
}
}
这是片段的功能,我可以在其中进行加载
This function is being called for fetching data from remote
private fun getTransactionHistory() {
swipeContainer.enableDisableSwipeRefresh(true)
enableShowMore(false)
//logic to load web data and hide show swiperefresh
//when load is success call this function enableShowMore(true)
//when load is failed call this function enableShowMore(false)
}
@Synchronized
private fun enableShowMore(enable: Boolean) {
val viewHolder = recyclerView.findViewHolderForAdapterPosition(transactionList.size)
if (viewHolder is MyTransactionAdapter.FooterViewHolder) {
viewHolder.enableShowMore(enable)
}
}
//这是单击页脚视图时的已实现方法 重写fun onLoadMoreClicked(){ fetchTransactionListFromServer() } 我需要用这种方法做些什么,因为用户可以一次单击多个页脚,而无需让应用加载远程数据. 如果不让我知道,希望您了解情况.预先感谢
//this is the implemented method when footer view is clicked override fun onLoadMoreClicked() { fetchTransactionListFromServer() } Is there anything I need to do with this approach because users are able to click the footer multiple at a time without letting the app to load the remote data. Hope you have understood the situation if not let me know. Thanks in advance
这是我实现的屏幕截图
Here is the screenshot what I have achieved
推荐答案
能否请您尝试将函数代码放入 onBindViewHolder()中的 enableShowMore()中或进行设置 FooterViewHolder 的 holder.bind 并将代码放入活页夹函数中.
Could you please try to put your function code inside enableShowMore() in onBindViewHolder() or set holder.bind for FooterViewHolder and put code inside binder function.
让我知道它是否有用.
这篇关于更改recyclerview的页脚itemView的视图文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!