如何在同一 activity 中为两个不同的 recyclerview 设置两个不同的 onClick 侦听器

How to set two different onClick listeners for two different recyclerviews in same activity

我有一个 Activity 和两个不同的回收器 views.and 两个适配器。

  1. 抽屉适配器。 我已经设置了一个 onClickListner 接口并在 Activity class.
  2. 中实现了

DrawerAdapter

public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.DrawerItemViewHolder>{

    Context mContext;
    int layoutResID;
    ArrayList<DrawerItem> menuList;
    onItemClickListner mListner;
    DrawerLayout mDrawerLayout;
    RecyclerView mDrawerList;
    WebView webViewLayout;
    LinearLayout mDrawerListParent;
    Toolbar toolbar;

    int[] drawer_icons=new int[]{R.drawable.dr_plus,R.drawable.dr_plus_square,R.drawable.dr_search,R.drawable.dr_tasks,R.drawable.dr_dashboard
            ,R.drawable.dr_buy_dashboard,R.drawable.dr_add_user2,R.drawable.dr_table,R.drawable.dr_status_map,R.drawable.dr_trackchanges
            ,R.drawable.dr_templates};



    public interface onItemClickListner{
        void onItemClick(int position);
    }



    public class DrawerItemViewHolder extends RecyclerView.ViewHolder{
       TextView itemTitle;
       ImageView itemIcon;
       onItemClickListner listner;
       RelativeLayout dItemParentLayout;

        public DrawerItemViewHolder(View itemView, final onItemClickListner listner) {
            super(itemView);
            itemTitle=itemView.findViewById(R.id.menu_item_title);
            itemIcon=itemView.findViewById(R.id.menu_item_icon);

            this.listner=listner;
            dItemParentLayout=itemView.findViewById(R.id.d_item);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if (listner!=null){
                       int position=DrawerItemViewHolder.this.getAdapterPosition();
                       if (position!=RecyclerView.NO_POSITION){
                           listner.onItemClick(position);
                                                  }
                   }
                }
            });
        }
    }//ViewHolder

    public DrawerAdapter(int resource, ArrayList<DrawerItem> itemList, WebView webViewLayout,
                         DrawerLayout mDrawerLayout, RecyclerView mDrawerList, LinearLayout drawerListParent,
                         Toolbar pToolbar,onItemClickListner listner){

        layoutResID=resource;
        menuList=itemList;
        mListner=listner;
        this.mDrawerLayout=mDrawerLayout;
        this.mDrawerList=mDrawerList;
        this.webViewLayout=webViewLayout;
        this.mDrawerListParent=drawerListParent;
        toolbar=pToolbar;
    }



    //======================================================================================
    @Override
    public DrawerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_list_item,parent,false);
        DrawerItemViewHolder dvh=new DrawerItemViewHolder(v,mListner);
     return dvh;
    }
    //======================================================================================

    @Override
    public void onBindViewHolder(final DrawerItemViewHolder holder, final int position) {

        final DrawerItem currentItem=menuList.get(position);

       // holder.itemIcon.setImageResource(drawer_icons[position]);
        holder.itemTitle.setText(currentItem.getTitle());

        holder.dItemParentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ONCLICK EVENTS
                System.out.print("CLICKED AT:"+position +"\n");

                webViewLayout.setWebViewClient(new WebViewClient());
                toolbar.setSubtitle(currentItem.title);

//                toolbar.setLogo(R.drawable.car3);
                String xurl="someURL/"+currentItem.getLinkURL();
                webViewLayout.loadUrl(xurl);
                mDrawerLayout.closeDrawer(mDrawerListParent);
            }
        });
    }
    //======================================================================================

    @Override
    public int getItemCount() {
        return menuList.size();
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }


}
  1. 通知适配器。 我在 notificationAdapter 的 onBindViewHolder 中设置了一个 onclick 监听器。

NotificationAdapter

    public class NoificationAdapter extends RecyclerView.Adapter<NoificationAdapter.NotificationHolder> {
    private ArrayList<NotificationItem> notificationItems;
    private onItemClickListner mListner;
    private int layoutResID,notificationposition;
    Context myContext;

    String notificationDate,notificationRecType;



    String []myc={"#43A047","#C62828","#039BE5","#F57C00","#4A148C","#43A047",
            "#039BE5","#C62828","#43A047","#C62828","#039BE5","#F57C00","#4A148C",
            "#43A047","#039BE5","#C62828"};


    public interface onItemClickListner{
        void onItemClick(int position);
    }

    public static class NotificationHolder extends RecyclerView.ViewHolder{
        TextView mNotificationTextView;
        RelativeLayout mNotificaionHolderLayout;
        ImageView imageDelete;
        String mNotificationURL;
        String mNotificationDate;
        String mNotificationRT;
        onItemClickListner listner;

        public NotificationHolder(View itemView,final onItemClickListner listner) {
            super(itemView);
            this.listner=listner;
            mNotificationTextView=itemView.findViewById(R.id.NotificationTextView);
            mNotificaionHolderLayout=itemView.findViewById(R.id.notification__item_container);
            imageDelete=itemView.findViewById(R.id.notification_delete_image);
            mNotificationRT=null;
            mNotificationURL=null;
            mNotificationDate=null;

//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (listner != null) {
//                        int position = NotificationHolder.this.getAdapterPosition();
//                        if (position != RecyclerView.NO_POSITION) {
//                            listner.onItemClick(position);
//                        }
//                    }
//                }
//            });
        }
    }//END OF NOTIFICATION_VH

    public NoificationAdapter(Context context,int resource,ArrayList<NotificationItem> notificationList,onItemClickListner listner){
        myContext=context;
        layoutResID=resource;
        notificationItems=notificationList;
        mListner=listner;
        notificationposition=0;
    }

    @Override
    public NotificationHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_notificationitem,parent,false);
        NotificationHolder evh=new NotificationHolder(v,mListner);
        return evh;
    }

    @Override
    public void onBindViewHolder(NotificationHolder holder, int position) {

        final NotificationItem currentItem=notificationItems.get(position);

        notificationposition=currentItem.getPositionNumber();
        if (notificationposition>=myc.length){
            if (notificationposition%2==0){
                holder.mNotificaionHolderLayout.setBackgroundColor(Color.parseColor(myc[0]));
                holder.imageDelete.setBackgroundColor(Color.parseColor(myc[0]));
            }else {
                holder.mNotificaionHolderLayout.setBackgroundColor(Color.parseColor(myc[2]));
                holder.imageDelete.setBackgroundColor(Color.parseColor(myc[2]));
            }
        }

        holder.mNotificaionHolderLayout.setBackgroundColor(Color.parseColor(myc[notificationposition]));
        holder.imageDelete.setBackgroundColor(Color.parseColor(myc[notificationposition]));

        holder.mNotificationTextView.setText(currentItem.getNotification_name());
        holder.mNotificationURL=currentItem.getNotification_link();
        holder.mNotificationDate=currentItem.getNotification_date();
        holder.mNotificationRT=currentItem.getNotification_rT();

        if (!holder.mNotificationURL.isEmpty()){
            holder.imageDelete.setVisibility(View.INVISIBLE);
        }

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

                deleteNotification(currentItem);


            }
        });

    }

    @Override
    public int getItemCount() {
        return notificationItems.size();
    }



//Delete from View
    public void deleteNotification(NotificationItem todelete){
        int notificationPosition=notificationItems.indexOf(todelete);
        notificationItems.remove(notificationPosition);
        notifyItemRemoved(notificationPosition);
        notifyItemChanged(notificationPosition);
    }

}

当我在我的 activity 中添加这两个适配器时,如果我先滚动通知,然后抽屉想要 scroll.vice 相反,只有一个监听器在工作,第一个使用的监听器。

Activity.java

       public class NavigationDrawerActivity extends AppCompatActivity implements DrawerAdapter.onItemClickListner,NoificationAdapter.onItemClickListner{

 //in fill drawer function   
         drawerAdapter = new DrawerAdapter(R.layout.drawer_list_item, drawerItemList,
                                webViewLayout, mDrawerLayout, mDrawerList,
                                mDrawerParent,mToolbar,
                                NavigationDrawerActivity.this);
                        drawerAdapter.setHasStableIds(true);
                        mDrawerList.setAdapter(drawerAdapter);
     //in fill notification function   
                           nfAdapter=new NoificationAdapter(NavigationDrawerActivity.this,
                        R.id.listView_Notific_Link, notificationList,nListner);
                lvNotification.setAdapter(nfAdapter);
    }

我该如何解决这个问题?

将DrawerAdapter.DrawerAdapter改为:

public interface onItemClickListner {
    void onDrawerItemClick(int position);
}

发生的事情是,在您的 Activity 中,您有一个名为 onItemClick 的方法。您在调用该方法的两个适配器中设置了一个 clicklistener。因此,当单击它们时,两个适配器最终都会调用完全相同的 onItemClick。您需要做的就是找出一种方法来判断单击了哪个适配器。有很多方法可以做到这一点,我认为上面的一种是最简单的。

这几乎不需要更改代码。

监听器 1

interface Listener1 {
 
    function1();

}

监听器2

interface Listener2 {
 
    function1(){}
   
}

Activity代码

YourActivity implements Listener1, Listener2 {
    
     // Override functionality 
     function1(){}
 
     function2(){}

    // Pass reference to Adapters like this 

    drawerAdapter = new DrawerAdapter(R.layout.drawer_list_item, drawerItemList,
                            webViewLayout, mDrawerLayout, mDrawerList,
                            mDrawerParent,mToolbar,
                            YourActivity.this);
                    

}

最重要的是在适配器中对您的侦听器进行类型转换

NotificationAdapter {
   
     Listener1 listener1; 
      
     NotificationAdapter(whatever, .... , listener1){
       
       listener1 = (Listener1)listener1     // Because by default it will be YourActivity. This is how inheritance works. 
      
     }
}

Another adapter以Listener2为实例变量做同样的事情