问题
Hi I have a condition where I have to add different viewType with text
show more
or loading...
at the bottom of the recyclerview
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.
Here is my adapter class
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
}
}
}
And here is the fragment's function where I do the loading stuff
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)
}
}
//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
回答1:
Can you try disabling the footer view button once the user clicks on it?
fun enableShowMore(enable: Boolean) {
if( enable ) {
itemView.btnShowMore.text = "show more"
} else {
itemView.btnShowMore.text = "loading..."
}
itemView.btnShowMore.isEnabled = enable
}
Update :
//In the adapter
mIsFooterEnabled = false
//In the View holder
fun enableShowMore() {
if( mIsFooterEnabled ) {
itemView.btnShowMore.text = "show more"
} else {
itemView.btnShowMore.text = "loading..."
}
itemView.btnShowMore.isEnabled = mIsFooterEnabled
}
//In your activity / fragment
private fun enableShowMore(enable: Boolean) {
adapter.mIsFooterEnabled = enable
adapter.notifyDataSetChanged()
}
回答2:
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.
Let me know if it is useful or not.
来源:https://stackoverflow.com/questions/55354898/change-the-view-text-of-footer-itemview-of-recyclerview