У меня есть Активность с двумя различными видами переработчика. И для них два Адаптера.
- 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);
}
}
- NotificationAdapter.
я установил список для onclick в onBindViewHolder для уведомлений.
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);
}
}
когда я добавил эти два адаптера в свою деятельность, если я сначала прокручиваю уведомления, а затем ящиком хочу 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);
}
Как я могу решить это?