【问题标题】:Recyclerview not refreshing after removing an item删除项目后Recyclerview不刷新
【发布时间】:2019-02-21 06:36:50
【问题描述】:

删除项目后,我的回收站视图未更新。 这个 recyclerView 在一个片段内。 我已经尝试了所有方法,但没有任何效果。

片段类中的适配器声明

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

}

当我调试项目时,我可以看到该项目实际上是从 ArrayList 中删除。但在回收视图中没有更新。

删除后,如果recyclerview被滚动,被删除的item从recyclerview中移除。但不是不滚动。

【问题讨论】:

  • 是的,即使我遇到了同样的问题
  • @bk7 你找到解决办法了吗?
  • 是的,我做了一些研究
  • 分享您的 NotificationsTabAdapter 构造函数代码,我将发布解决方案
  • 我认为您必须在片段中设置 notifydatasetchanged() 并使用回调并再次将适配器设置为您的 recyclerview。

标签: android android-recyclerview


【解决方案1】:

试试这个。希望对你有用。

notificationItemHolder.imageDelete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
           notificationItems.remove(position);
           notifyDataSetChanged();
    }

【讨论】:

  • 可能是项目对象的问题(它可能引用不同的对象)。您可以发布完整的适配器类吗?所以我们可以帮助你
【解决方案2】:

在您的 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
public void onItemsDeleted(final int position) {
    notificationsTabAdapter.notifyDataSetChanged();
    recyclerView.post(new Runnable() {
        @Override
        public void run() {
            recyclerView.smoothScrollToPosition(position);

        }
    });
}

【讨论】:

  • 我做到了,现在我在界面中的 onNotificationItemDelete 回调上得到一个空点异常
  • 您是否在片段中实现了 NotificationsTabAdapterListener?
  • 并在 NotificationsTabAdapter 构造函数中传递它
  • 是的,您传递的是 cListner 而不是 this
  • 是的,是的,...知道了
【解决方案3】:

试试这个删除功能

public void deleteNotification(NotificationItemm todelete) {
    notificationItems.remove(todelete);
    notifyDataSetChanged();

}

【讨论】:

  • 不,这和我写的一样,从 dataList 中删除,但 recyclerview 不刷新
  • 告诉我如何将列表传递给 recycleView 适配器
  • notificationsTabAdapter = new NotificationsTabAdapter(getContext(), R.id.notificationsRecyclerView, notificationItemsList, cListner);在此行上,notificationItemsList 的类型是什么以及您如何在列表中添加数据,请分享
  • 在 Activity/Fragment 代码中尝试这个,而不是在 RecyclerView 适配器代码中。 mAdapter.notifyDataSetChanged();
  • notificationItemsList 是另一个类 NotificationItemm 的对象的 ArrayList。
【解决方案4】:

作为解决方法,您可以在 FragmentClass 中调用一个方法,该方法会在您的适配器中加载新列表(已删除项目)。从您的适配器调用此方法

public void MethodInFragmentClass(NotificationItemm todelete)
{
/*
... delete item 
*/
notificationsTabAdapter = new NotificationsTabAdapter(getContext(), R.id.notificationsRecyclerView,
                notificationDeletedItemsList, cListner);

                recyclerView.setAdapter(notificationsTabAdapter);
}

【讨论】:

    【解决方案5】:

    在您的适配器中使用这样的回调:

    private ICallback mICallback;
    
        public interface ICallback {
    
          void deleteItem(int position);
        }
        public SettingRecyclerViewAdapter(SettingMediator settingMediator, ICallback ICallback) {
            mICallback = ICallback;
            mSettingMediator = settingMediator;
        }
    

    在你的 faragment notifydatasetchange 和 update recyclerview 中像这样:

    public class YourFragment implements SettingContract.View, SettingRecyclerViewAdapter.ICallback {
    .
    .
    .
        @Override
        public void deleteItem(int position) {
    
                    //delete item from your list here
            mSettingRecyclerViewAdapter = new SettingRecyclerViewAdapter(yourList, this);
            mRecyclerView.setAdapter(mSettingRecyclerViewAdapter);
            mSettingRecyclerViewAdapter.notifyDataSetChanged();
        }
    
    
    }
    

    【讨论】:

      【解决方案6】:

      试试这个:

          notificationItemHolder.imageDelete.setTag(holder);
          notificationItemHolder.imageDelete.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  NotificationItemHolder viewholder = (NotificationItemHolder) v.getTag();
                  notificationItems.remove(viewholder.getAdapterPosition());
                  notifyDataSetChanged();
              }
          });
      

      【讨论】:

        【解决方案7】:

        您必须将从适配器类中的 onclick 侦听器获取的位置解析为接口方法(onitemClicked()) 然后,将接口类实现为 Fragment 类,并使用 [ listname.remove(position)) ] 删除我们在接口方法上的位置 最终,使用 adaptername.notifyDataSetChanged() 更新 recyclerview UI;

        第一步:创建接口类

        public interface RecyclerviewItemClickListener {
        
        
            void onitemClicked(View v, int position);
        }
        

        第2步:将位置传递给适配器类中的接口方法

        notificationItemHolder.imageDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteNotification(currentItem);
                //use interface method & pass the position of the list to fragment for update UI .
                recyclerviewItemClickListener.onitemClicked(v,position);
            }
        });
        

        第三步:实现fragment中的接口类&方法

        @Override
            public void onitemClicked(View v, int position) {
        
                Listname.remove(Listname.get(position));
                RecyclerviewAdaptername.notifyDataSetChanged();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-12
          相关资源
          最近更新 更多