删除项目后,“我的回收器”视图未更新。此recyclerView位于片段内部。我试过各种方法,但都不管用。
片段类中的适配器声明
notificationsTabAdapter = new NotificationsTabAdapter(getContext(), R.id.notificationsRecyclerView,
notificationItemsList, cListner);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(notificationsTabAdapter);
回收服务适配器:
public class NotificationsTabAdapter extends RecyclerView.Adapter<NotificationsTabAdapter.NotificationItemHolder> {
Boolean debug = false;
public static final String NOTIFICATION_ADAPTER = "NotificationAdapter";
private ArrayList<NotificationItemm> notificationItems;
private int layoutResID;
private int notificationposition;
private Context myContext;
public onNotificationItemClickListner mListner;
public interface onNotificationItemClickListner {
void onNotificationItemDelete(int position);
}
public NotificationsTabAdapter(Context context, int resource, ArrayList<NotificationItemm> notificationList,
onNotificationItemClickListner listner) {
myContext = context;
layoutResID = resource;
notificationItems = notificationList;
notificationposition = 0;
this.mListner = listner;
}
@NonNull
@Override
public NotificationItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.notifications_tab_item, viewGroup, false);
NotificationsTabAdapter.NotificationItemHolder evh = new NotificationsTabAdapter.NotificationItemHolder(view, mListner);
return evh;
}
@Override
public void onBindViewHolder(@NonNull final NotificationItemHolder notificationItemHolder, final int position) {
final NotificationItemm currentItem = notificationItems.get(position);
notificationItemHolder.mNotificationTextView.setText(currentItem.getNotification_name());
notificationItemHolder.mNotificationURL = currentItem.getNotification_link();
notificationItemHolder.mNotificationDate = currentItem.getNotification_date();
notificationItemHolder.mNotificationRT = currentItem.getNotification_rT();
notificationItemHolder.mNotificaionHolderLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
//to delete the notification
notificationItemHolder.imageDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteNotification(currentItem);
mListner.onNotificationItemDelete(position);
}
});
}
@Override
public int getItemCount() {
return notificationItems.size();
}
//Delete from View
public void deleteNotification(NotificationItemm todelete) {
int notificationPosition = notificationItems.indexOf(todelete);
notificationItems.remove(notificationPosition);
notifyItemRemoved(notificationPosition);
notifyItemChanged(notificationPosition);
notifyDataSetChanged();
notifyItemRemoved(notificationPosition);
notifyItemChanged(notificationPosition);
if (notificationItems.isEmpty()) {
}
}
/**
* VIEW HOLDER =================================================================================
**/
public class NotificationItemHolder extends RecyclerView.ViewHolder {
RelativeLayout mNotificaionHolderLayout;
RelativeLayout notificationParentRelative;
ImageView imageDelete;
TextView mNotificationTextView;
String mNotificationURL;
String mNotificationDate;
String mNotificationRT;
public NotificationItemHolder(@NonNull View itemView, onNotificationItemClickListner listner) {
super(itemView);
mNotificationTextView = itemView.findViewById(R.id.NotificationTextView);
mNotificaionHolderLayout = itemView.findViewById(R.id.notification__item_container);
imageDelete = itemView.findViewById(R.id.notification_delete_image);
notificationParentRelative = itemView.findViewById(R.id.rlNotificLayout);
mNotificationRT = null;
mNotificationURL = null;
mNotificationDate = null;
}
}
}
调试项目时,我可以看到该项实际上正在从ArrayList中删除。但不在循环视图中更新。
删除后,如果滚动回收站视图,则从回收站视图中删除已删除的项。但不是没有滚动。
尝试删除功能
public void deleteNotification(NotificationItemm todelete) {
notificationItems.remove(todelete);
notifyDataSetChanged();
}
在NotificationsTabAdapter中,进行以下更改
NotificationsTabAdapterListener notificationsTabAdapterListener;
public interface NotificationsTabAdapterListener { // create an interface
void onItemsDeleted(int position); // create callback function
}
public NotificationsTabAdapter(Context context, int resource, ArrayList<NotificationItemm> notificationList,
NotificationsTabAdapterListener notificationsTabAdapterListener) {
myContext = context;
layoutResID = resource;
notificationItems = notificationList;
notificationposition = 0;
this.notificationsTabAdapterListener = notificationsTabAdapterListener;
}
notificationItemHolder.imageDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//perform normal remove operation here
notificationItems.remove(position);
notificationsTabAdapterListener.onItemsDeleted(position);
}
});
并在片段中实现NotificationsTabAdapterListener,在override方法中使用以下代码
@Override
public void onItemsDeleted(final int position) {
notificationsTabAdapter.notifyDataSetChanged();
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(position);
}
});
}
试试这个。希望对你有用。
notificationItemHolder.imageDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notificationItems.remove(position);
notifyDataSetChanged();
}
我有一个RecyclerView,这些物品应该在刷卡后移除。我用ItemTouchHelper类解决了这个问题。在onchildraw方法中,我设置了一个红色背景,在列表项后面滑动时出现。最重要的是,在正确的网站上有一个垃圾图标。它工作正常,但是如果你开始刷卡,但不刷卡到底,然后又刷卡回到正常位置,垃圾桶的图标不会消失。但是,它只有在我滑动所选项目时才会消失,而不是在普通项目上。 下面是ItemT
我正在用AppWidgetHostViews填充RecyclerView。它们被排序一个比一个低,每个都有删除按钮旁边。这就是我如何设置RecyclerView和适配器(Event.Object是AppWidgetHostView): 这是保存AppWidgetHostViews的适配器: 因此它再次为适配器设置数据并通知数据集已更改。 但问题是,当我单击“删除”按钮时,项目没有被正确删除。Vie
但是建议的解决方案对我来说都不起作用。我从sqlite数据库中的RecycerView列表项,当我向左滑动时,相应的数据将成功地从数据库中删除。由于某些原因,昨天一切都工作得很好,删除的项从列表中消失了,但从今天开始,该项在RecycerView中仍然可见。下面是我的代码:
我已经从sqlite中填充了RecolyerView。当单击每一行时,row将从sqlite中删除,但删除后RecolyerView不显示更新的列表。回收器视图仅在再次启动活动后显示更新的列表。我的问题是,在没有刷新的情况下,从recylcerview中删除一个项目后,如何很快更新回收器视图。 下面是我的代码 SecondActivity.java DatabaseHelpher.java Dat
我在片段的底部有一个水平图像滑块。片段的顶部显示了一些细节。一旦用户点击底部的图像,想法是从图像滑块中删除该图像,并在片段中显示其信息。现在,信息显示出来,但图像不会从中删除。下面是我在最外层布局的中编码的内容。我试过了所有我能找到的相关答案,但都没有奏效。它们都在代码里。请让我知道我做错了什么或者错过了什么。 适配器的完整代码 }
我想要一个带有滑动手势的RecyclerView来触发一个动作,比如将滑动的项目标记为收藏夹。是否可以使用ItemTouchHelper但在滑动后禁用视图删除?一个常见的模式是滑动来显示某个按钮,但这不是想要的行为。