How to implement two OnClickListeners on RecyclerView item click?

天涯浪子 提交于 2019-12-25 19:01:26

问题


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!


回答1:


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



回答2:


You can change your BottomBar width same (unique) click using UIHandler or MainLoop,with which you can access the UI from your adapter. on your OnCreate:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 //.....
Handler uiHandle = new Handler(getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle b = (Bundle) msg.obj;
            int p = b.getInt("position");
            Toast.makeText(MainActivity.this, "Change here your BottomBAR",     Toast.LENGTH_SHORT).show();

        }
    };

.. and in your adapter ...

 yourobjectclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

           // example: first change color of my object that I have clicked
                ((TextView)v).setTextColor( mContext.getResources().getColor(R.color.blue_100));
                Bundle bundle = new Bundle();
               //i can store the position about click and
                bundle.putInt("position",position);

                Message message = handler.obtainMessage();
                message.obj = bundle;
                // i have access tu User Interface objects from here

           // next change my bottombar sending a message to UI
           handler.sendMessage(message);
                Toast.makeText(mContext, "onClick: su -> "+tvBikeName.getText(), Toast.LENGTH_SHORT).show();
            }
        });


来源:https://stackoverflow.com/questions/55983616/how-to-implement-two-onclicklisteners-on-recyclerview-item-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!