如何在同一活动中为两个不同的recyclerviews设置两个不同的onClick侦听器

时间:2018-10-31 07:04:12

标签: android android-recyclerview onitemclicklistener

我有一个带有两个不同回收者视图的“活动”,并且有两个“适配器”。

  1. DrawerAdapter。 我已经设置了onClickListner接口,并隐含在Activity类中。
  

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。 我已经在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.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);
    }

我该如何解决?

2 个答案:

答案 0 :(得分:0)

将DrawerAdapter.DrawerAdapter更改为此:

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

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

答案 1 :(得分:0)

这几乎不需要更改代码。

Listener1

interface Listener1 {

    function1();

}

Listener2

interface Listener2 {

    function1(){}

}

活动代码

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. 

     }
}

对另一个将Listener2作为实例变量的适配器做同样的事情。