0

I have in my MainActivity 3 RecyclerViews .

One of them in a bottom sheet and it is the main one (By Default the Bottom sheet is Open To Display this one ), in its adapter's onbind method I made an onClickListener so that I want when the user clicks on an item in it,

I want to go back to the main activity class to set To Start a method which it's rolled is to close the Bottom Sheet and set the data for the next recycling view (which will appear when the Bottom Sheet is closed)

..... The issue here is how to start this method from the onBind method's Listener and give it a parameter from this viewHolder as its name and some of its attributes

if there is something not clear please let me know

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {

        viewHolder.categoryImage.setImageResource(mRowOfCategories.get(position).getCategoryImage());
        viewHolder.categoryName.setText(mRowOfCategories.get(position).getCategoryName());
        viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });


    }
VIISHRUT MAVANII
  • 11,410
  • 7
  • 34
  • 49
  • Best and Simple way to create a public interface and implement in Activity and make setter in Adapter, then onClick user listener object to send callback back to Activity to perform Specific Operation. – Asad Mukhtar Oct 28 '19 at 21:01

4 Answers4

0

You can do that easily.

Define custom interface

public Interface CustomEventListener 
{
     public void MyEventListener(String message); //you can change parameters
}

In your adapter class

 public Adapter ......... {

        private CustomEventListener listener;

        public void setListener(CustomEventListener listener)
        { 
            this.listener = listener;
        }

        //Your onBind
       Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {

        viewHolder.categoryImage.setImageResource(mRowOfCategories.get(position).getCategoryImage());
        viewHolder.categoryName.setText(mRowOfCategories.get(position).getCategoryName());
        viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                  if (listener != null)
                        listener.MyEventListener("Message");
            }
        });    
    }

}

In your Activity when create Adapter add this code

 public void InitAdapter()
{

  yourAdapter = new Adapter(); // bloa bla bla   
  yourAdapter.setListener(new CustomEventListener()  {
      public void MyEventListener(String message)
      {
           // then do what you want
      }   
 }
}
Volkan Sonmez
  • 745
  • 3
  • 14
0

You need to use Listener for handling the click in your Adapter something like this:

private OnItemClickListener mListener;

public interface OnItemClickListener {
    void onItemClick(View view, int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;
}

then call the listener with:

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {

    ...

    viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // call the listener.
           mListener.onItemClick(v, viewHolder.getAdapterPosition());
        }
    });
}

then when you're using the adapter, set the listener with something like this:

adapter.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(View view, int position) {

       // do something with the view and position.
    }
});
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

Easiest:

You declare the method you want to call in your Activity. It has to be public:

public void method(){}

Then, in the Constructor of the Adapter, you pass a reference to the Activity as a parameter:

public MyAdapter(Activity activity){}

And, in the onBindViewHolder:

MyActivity mActivity=(MyActivity)activity;
mActivity.method();
Fustigador
  • 6,339
  • 12
  • 59
  • 115
0

You want your recycleviews to be aware of each other. You'll need a class, maybe MainActivity, that will keep the selected value, also clear it upon request, in between recycleviews. Then you'll need that value to propagate the next recycleview. If you want to be efficient then you'll use one recycleview and swap the data in between selections, and animate it so it looks like a new recycleview is created.

Haroun Hajem
  • 5,223
  • 3
  • 26
  • 39