简体   繁体   中英

How to access a TextView outside RecyclerView in Android?

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 ie 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"

在 recylerview 之外访问 textview

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?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM