1

As per the image, in my main activity, I used a fragment and inside this fragment there is a horizontal RecyclerView, with some CardView(4 shown in the image below). The TextView is outside RecyclerView on the parent fragment. What I want is, access the TextView from the RecyclerView i.e. if I click a card inside the RecyclerView, the corresponding card text will show in the TextView.

Example: If I click CardView2 the TextView text will be "card2"....If I click CardView3 the TextView text will be "card3"

access textview outside recylerview

This is my Adapter Class:

public class ScanCodeSliderAdapter extends RecyclerView.Adapter<viewHolder> {

    private List<ScanCodeCardModel> scanCodeCardModels;
    private Context context;


    public ScanCodeSliderAdapter(List<ScanCodeCardModel> scanCodeCardModels, Context context) {
        this.scanCodeCardModels = scanCodeCardModels;
        this.context = context;
    }

    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_scan_code_card_item, parent, false);
        return new viewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final viewHolder holder, final int position) {
       
        holder.scanCodeSingleItemCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public int getItemCount() {
        return scanCodeCardModels.size();
    }

}

This is my ViewHolder Class:

public class viewHolder extends RecyclerView.ViewHolder {

    public CardView scanCodeSingleItemCardView;

    public viewHolder(@NonNull View itemView) {
        super(itemView);
        
        scanCodeSingleItemCardView = itemView.findViewById(R.id.scanCodeSingleItemCardView);
    }


}

What else can I add here to do that?

null_override
  • 467
  • 3
  • 10
  • 30
Jeet Nath
  • 27
  • 1
  • 7

3 Answers3

2

The best way to do this is via a click listener which will allow you to set up a communication between your RecyclerView items and the widgets or views outside of the recycler View.

To set up a listener you can use this example as a guide:

interface OnItemsClickListener{
     void onItemClick(ScanCodeCardModel scanCodeCardModels);
}

then create a listener variable inside your adapter class:

private OnItemsClickListener listener  = null;

and aa public method that will handle the or do the listening between your activity or fragment and the RecycerView Adapter like this

void setOnItemClickListener(OnItemsClickListener listener){
     this.listener = listener;
}

now go back into your onBindViewHolder inside the onClick() override method

 @Override
public void onBindViewHolder(@NonNull final viewHolder holder, final int position) {
   
    ScanCodeCardModels item =  scanCodeCardModels[position]

    holder.scanCodeSingleItemCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();
           if(listener != null){
               listener.onItemClick(item)
           }
        }
    });
}

that's all you need to do inside your Adapter class. Then proceed to your Fragment or Activity class, and connect to the listener

adapter.setOnItemClickListener(new AdapterName.OnItemClickListener(){
      @Override
      public void onItemClick(ScanCodeCardModel scanCodeCardModels){
           //Set your TextView here when card is Clicked on
           textView.setText(scanCodeModels.name)
      }
})

And that's all.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chidi
  • 189
  • 2
  • 10
  • Thank You, its exactly what I wanted. Just one more question though... What is actually happening in '''if(listener != 0){listener.onItemClick(item);}'''. – Jeet Nath Sep 09 '20 at 11:14
  • First, the condition is to ensure the listener has been initialized and it's not null and when this is true we are calling onItemClick(item) passing our current selected item as a parameter for the activity listening to this event to make use of – Chidi Sep 09 '20 at 11:29
  • @JeetNath you can read more here https://antonioleiva.com/recyclerview-listener/ – Chidi Sep 09 '20 at 11:31
0

try adding onclick method that will access TextView object by findViewById and set text on it. OnClick method will have View view attribute so you can use it to know which element was clicked.

You should also have arrayadapter of some kind for your recycler view, you can add there some onclick methods that will use mainactivity as a listener.

0

Create a public method in fragment class. with parameter as string.
Now Under recyclerview textview click . call the fragment class method with its context. eg. context.methodName(parameter).
and into public method set textview text using parameter.

Jigar
  • 104
  • 7