Ad

How To Implement Two OnClickListeners On RecyclerView Item Click?

I'm quite confused since I need to use two OnClickListeners for two different needs. I have a Recyclerview which once any item of his, once pressed, needs to change and I've implemented that successfully using OnBindViewHolder.itemView.setOnClickListener

Now, I want that very same click to update my BottomAppBar and for that, I added an interface to my ItemHolder class. The issue is that now I once I click, due to the interface (I guess) nothing happens.

I know it might be a bit complicated to understand my situation so I add the following code to help with that:

public class MainActivity extends AppCompatActivity implements MultiViewTypeAdapter.IClickListener {

has this function:

 @Override
    public void onItemClick(View view, int position) {
        int type = adapter.getItemViewType(position);
        if (type == 0) {
            bar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_END);
            bar.replaceMenu(R.menu.bab_menu_chosen_project);
            fab.setImageDrawable(getDrawable(R.drawable.ic_message_white_24dp));
        }
        else{
            bar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_CENTER);
            bar.replaceMenu(R.menu.bab_menu_primary);
            fab.setImageDrawable(getDrawable(R.drawable.ic_reply_black_24dp));
        }

    }

the interface at the adapter looks like this:

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

what I'm currently doing on public static class SelectedProjectItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener { is this:

@Override
        public void onClick(View v) {
            listener.onItemClick(v, getAdapterPosition());
        }

however, what I want to run is what I'm doing on

public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

is:

((SelectedProjectItemHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dataSet.remove(listPosition);
                        dataSet.add(listPosition, unselectedCards.get(listPosition));
                        notifyItemChanged(listPosition);
                    }
                });

I wish two things will happen from one click - the item on Recyclerview will change using the last piece of code I described, and the menu in my MainActivity will change using the first piece of code I described.

THANKS HELPERS!

Ad

Answer

Remove below code from RecyclerView.ViewHolder

@Override
public void onClick(View v) {
    listener.onItemClick(v, getAdapterPosition());
}

And add listner event in "itemView" click as below:

((SelectedProjectItemHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(listener != null){
                            listener.onItemClick(v, getAdapterPosition());
                        }
                        dataSet.remove(listPosition);
                        dataSet.add(listPosition, unselectedCards.get(listPosition));
                        notifyItemChanged(listPosition);
                    }
                });
Ad
source: stackoverflow.com
Ad