grantespo
grantespo

Reputation: 2269

Update TextView in Fragment when RecyclerView's data changes

In my Fragment, I have 2 views. A Textview and a RecyclerView. The Textview essentially displays the current size of the RecyclerView. So, when a row is removed in the adapter class, I need to update the TextView's value accordingly.

The row is removed successfully when removeBtn is clicked, but I need to update the TextView in the Fragment accordingly.

FRAGMENT

                titleText.text = "SIZE (" + arrayStringList().size.toString() + ")"
                //...
                //... 
                //Set RecyclerView Adapter
                mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
                val adapter = MRecyclerView(context!!, arrayStringList)
                mRecyclerView.adapter = adapter

RECYCLERVIEW

holder.removeBtn{ 
   mData.removeAt(position)
   notifyItemRemoved(position)
 }

Is there some sort of listener that I can put in my fragment to detect when the data changes? Or is there a way to send data from the recyclerview back to the fragment when removeBtn is clicked?

Upvotes: 2

Views: 988

Answers (3)

martinseal1987
martinseal1987

Reputation: 2419

"Or is there a way to send data from the recyclerview back to the fragment when removeBtn is clicked?" the answer is yes

One approach is to pass your adapter your own click listener (an interface), this way you can control the click from the fragment something like this,

A1) create an interface

public interface ItemTouchListener {

    void onCardClick(View view, int position);

    boolean onCardLongClick(View view, int position);

}

A2) implement this interface in your fragment

public class YourFragment extends Fragment implements ItemTouchListener

A3) implement the methods you created in your fragment

@Override
public void onCardClick(View view, int position) {
    if (view.getId() == R.id.removeBtn){
       //update your text
    }else{
      //other buttons
    }
}

@Override
public boolean onCardLongClick(View view, int position) {
  if (view.getId() == R.id.removeBtn){
     //update your text
  } else {
    //other buttons
  }
    return true;
}

A4) create this interface in your adapter and set it to your button

private ItemTouchListener onItemTouchListener;

public YourAdapter(List<Object> list, ItemTouchListener onItemTouchListener) {
    this.onItemTouchListener = onItemTouchListener;
    this.list = list;
}

removeBtn = view.findViewById(R.id.removeBtn);
        removeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemTouchListener.onCardClick(v, getAdapterPosition());
            }
        });
        removeBtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                onItemTouchListener.onCardLongClick(view, getAdapterPosition());
                return true;
            }
        });

A5) and now when you create your adapter it will ask for the ItemTouchListener which you can give it by just passing 'this'

 YourAdapter adapter = new YourAdapter(this);

A6) you may also want to give your adapter a method something like getCount which return the list size

public int getCount(){
  list.size();
}

and then you can call this like myAdapter.getCount

Upvotes: 0

Mini Chip
Mini Chip

Reputation: 969

Create a interface in recyclerView class , and implement that interface in fragment . Once you click the remove button , call this interface . In Fragment class , update the text view with adapter.getItemCount.

In Adapter

interface ItemCallback{
    void updateTextView();
}

This interface will be implemented in fragment class ,where you can update your textView with itemCount .

 public class Fragment implements Adapter.ItemCallback{

  @Override
  public void updateTextView() {
   tvTextView.setText(adapter.getItemCount()); // This will return the current item count of adapter
   }
  }

Upvotes: 1

Abdul Waheed
Abdul Waheed

Reputation: 4678

You can do this via call back. Make an interface and implement that on Fragment. And pass the reference of the interface to the adapter. When you trigger the event to delete item from recycler view call the interface method (That you implemented on Activity). And in that call back method, you need to set the value to your text view. To set value on TextView, you can call size() method and set the value whatever is returned from it. For example the List you are passing to the adapter is mData. When you get call back the call size method on the reference and set the value as mentioned below code.

titleText.setText(String.valueOf(mData.size())).

Hope this will help you.

NOTE: You need to call String.valueOf method because size method returns integer value so it needs to be cast.

Upvotes: 0

Related Questions