我有一個tabLayout和兩個選項卡的活動。在每個選項卡中,我有一個不同的片段,其中包含由RecyclerView適配器管理的元素列表(RecyclerView)。對於每個列表中的每個元素,我都有一個從列表中刪除元素的按鈕。Android TabLayout更改標籤後notifyDataSetChanged()
該按鈕的點擊監聽器設置在適配器中。出現問題時,我在第二個標籤中,我從列表中刪除一個元素。在notifyDatSetChanged()之後,當前選項卡移動到第一個選項卡,我不想要那個!
這是我的代碼: 這是我的聲明,並在2個不同的片段設置適配器:
FRAG 1
adapterList = new NotificationListAdapter(getContext(), R.layout.notification_list_element, teamsLeader, null, leadershipTeam);
list.setAdapter(adapterList);
FRAG 2
adapterList = new NotificationListAdapter(getContext(), R.layout.notification_list_element, teamsMember, membershipTeam, null);
list.setAdapter(adapterList);
這是我的適配器的構造函數:
public NotificationListAdapter(Context context, int resource, List<DBTeam> notifications, ArrayList<MembershipTeam> membershipTeam, ArrayList<TeamInfo> leadershipTeam) {
this.notifications = notifications;
this.context = context;
this.resource = resource;
this.membershipTeam = membershipTeam;
this.leadershipTeam = leadershipTeam;
}
在我的onBindViewHolder中,我有一個用於不同視圖類型的開關盒: Mi imageView removeItem有一個調用方法的clickListener。
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
ImageView removeItem;
LinearLayout getItemDetail;
switch (holder.getItemViewType()) {
case CHECKIN:
BaseHolder vh1 = (BaseHolder) holder;
configureViewHolder(vh1, position, context.getString(R.string.notification_checkin_title), R.drawable.notification_checkin);
removeItem = vh1.getRemoveItemImage();
getItemDetail = vh1.getDetail_layout();
removeItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeItem(context, notifications, position);
}
});
getItemDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getMemberItemDetail(context, notifications, position);
}
});
break;
AND SO ON
這是RemoveItem方法調用點擊監聽器:
private void removeItem(Context context, List<DBTeam> notifications, int position){
MyRepository.getInstance(context).updateNotificationDBTeamSync(notifications.get(position).getNotificationId(), true);
notifications.remove(position);
this.notifyDataSetChanged();
}
我怎樣才能解決這個問題?
MyRepository.getInstance(context).updateNotificationDBTeamSync(notifications.get(position).getNotificationId(), true);
在
private void removeItem(Context context, List<DBTeam> notifications, int position){
MyRepository.getInstance(context).updateNotificationDBTeamSync(notifications.get(position).getNotificationId(), true);
notifications.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, notifications.size());
}
被調用,其獲取(使用領域)從DB數據我的活動方法:
這幾乎是不可能幫助你完全根據您所提供的信息。至少顯示您的RecyclerView適配器的代碼,包括點擊監聽器。 –
嗨,獅子座,我編輯了答案 – denno