0

I am trying to implement a "Pin Message" functionality on the chat app I'm developing.

Chat Activity looks like this:

enter image description here

I have a TextView above the chat RecyclerView and would like to set the text of that to the TextView value inside the RecyclerView. I can get the string value of what's inside the RecyclerView by using a PopupMenu (inside its adapter class) by showing it in a Toast for now.

How should I implement this? Thank you!

P.S. I'm still using Java.

2 Answers2

1

I finally found a solution for this one. Link to SO post here -> LINK

0

First create an interface called PinMessageListener

public interface PinMessageListener {

    void onPin(String value);

}

second, implement this interface in your activity or fragment

public class YourActivityOrFragment extends AppCompatActivity implement PinMessageListener {
    ...
    ...
    ...
    @Override
    public void onPin(String value) {
        yourTextView.setText(value);
    }
    ...
}

third, pass this interface in your recycler adapter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    //this refers to PinMessageManager
    YourRecyclerViewAdapter adapter = new YourRecyclerViewAdapter(this);
    ...
}

your recycler adapter must look like this:

public YourRecyclerViewAdapter extends RecyclerView.Adapter<YourViewHolder> {

    private final PinMessageListener pinMessageListener;
    
    public YourRecyclerViewAdapter(PinMessageListener pinMessageListener) {
        this.pinMessageListener = pinMessageListener;
    }

    onCreateViewHolder(...) {
        return new YourViewHolder(pinMessageListener);
    }

and your viewHolderClass must be look like this:

public YourViewHolder extends RecyclerView.ViewHolder {
    private final PinMessageListener pinMessageListener;
    public YourViewHolder(View rootView) {
        super(rootView);
        this.pinMessageListener = pinMessageListener;
    }

    public void bind(ModelClass model) {
        yourTextView.onClickListener((view) -> pinMessageListener.onPin(yourTextView.getText().toString());
}

And that's it.

OneDev
  • 557
  • 3
  • 14
  • I'm sorry, but I got lost in the RecyclerAdapter and viewHolderClass part. How can I return the pinMessageListener in the ViewHolder to the onCreateViewHolder if I have a viewType declared in the onCreateViewHolder? For the ViewHolder class, how do I implement the bind method? How does it connect with my PopupMenu inside the onBindViewHolder? – Dennis Manicani Jr. Nov 22 '22 at 07:22
  • Create a view holder class that has one argument in constructor and pass PinMessageListener in your viewholder class constructor, bind method is optional, implement it your self instead bind method – OneDev Nov 24 '22 at 15:32
  • Hello, I found a similar solution to what you provided. Thank you for the help! – Dennis Manicani Jr. Nov 25 '22 at 06:32