0

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 screenshot

Cristan
  • 12,083
  • 7
  • 65
  • 69
Ram Mandal
  • 1,899
  • 1
  • 20
  • 35

2 Answers2

0

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()
}
Alok Omkar
  • 628
  • 1
  • 9
  • 18
  • This is the question I asked. I am not able to disable the footer view button when I clicked on this view. – Ram Mandal Mar 27 '19 at 04:24
  • itemView.btnShowMore.isEnabled = enable, did you add this line and check? Is it still not getting disabled? – Alok Omkar Mar 27 '19 at 06:30
  • main thing is the enableShowMore() function is never called – Ram Mandal Mar 27 '19 at 07:55
  • Check if the viewholder is null at the point, if it is returning null, you'll need an alternate way to resolve this. I'll post a different solution if it's null. – Alok Omkar Mar 27 '19 at 08:05
  • ViewHolder is not null. The viewholder called is instance of ListViewHolder but I required the instanceo f FooterViewHolder. By the mean time I have done with alternate ways. but in this case I have added null at last to the list while data is fetched successfully and checked the item in the adapter as ` val da = transactionList[position] return if (da == null) { viewTypeFooter } else { viewTypeList }` – Ram Mandal Mar 27 '19 at 08:25
  • sure I will give a try. – Ram Mandal Mar 27 '19 at 09:20
0

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.

  • @Vagish I just provided suggestion to fix it. If it does not work then i can provide more information along with code. **Ram Mandal** let me know if it does not work. – Sahil Kothari Mar 27 '19 at 13:07