Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

android - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
                    }
                });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...