Recyclerview not refreshing after removing an item

  • A+
Category:Languages

My Recycler view is not updating after an item is removed. This recyclerView is inside a fragment. I have tried every method and nothing works.

Adapter declaration in fragment class

    notificationsTabAdapter = new NotificationsTabAdapter(getContext(), R.id.notificationsRecyclerView,                 notificationItemsList, cListner);         layoutManager = new LinearLayoutManager(getContext());         recyclerView.setLayoutManager(layoutManager);         recyclerView.setAdapter(notificationsTabAdapter); 

RecyclerViewAdapter:

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;     } } 

}

When I debug the project, I can see that the item is actually removing from the ArrayList.But not updating in recycled view.

After deletion, if the recyclerview is scrolled, the deleted item is removed from the recyclerview.But not without scrolling.

 


In your NotificationsTabAdapter make the following changes

   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);         }     }); 

And Implement NotificationsTabAdapterListener in you fragment and in override method use the following code

        @Override public void onItemsDeleted(final int position) {     notificationsTabAdapter.notifyDataSetChanged();     recyclerView.post(new Runnable() {         @Override         public void run() {             recyclerView.smoothScrollToPosition(position);          }     }); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: