Ma vue Recycleur ne se met pas à jour après la suppression d'un élément. Ce recyclerView est à l'intérieur d'un fragment. J'ai essayé toutes les méthodes et rien ne fonctionne.
Déclaration d'adaptateur dans la classe de fragment
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;
}
}
RecyclerViewAdapter:
notificationsTabAdapter = new NotificationsTabAdapter(getContext(), R.id.notificationsRecyclerView,
notificationItemsList, cListner);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(notificationsTabAdapter);
}
Lorsque je débogue le projet, je peux voir que l'élément est en train de se retirer de la ArrayList, mais pas de mise à jour en vue recyclée.
Après la suppression, si la vue de recyclage défile, l'élément supprimé est supprimé de la vue de recyclage, mais pas sans défilement.
7 Réponses :
Essayez ceci pour votre fonctionnalité de suppression
public void deleteNotification(NotificationItemm todelete) {
notificationItems.remove(todelete);
notifyDataSetChanged();
}
Non, cela fonctionne comme je l'ai écrit, en supprimant de dataList mais recyclerview ne rafraîchit pas
Dites-moi simplement comment ont transmis la liste à l'adaptateur recycleView
notificationsTabAdapter = new NotificationsTabAdapter (getContext (), R.id.notificationsRecyclerView, notificationItemsList, cListner); sur cette ligne quel est le type de notificationItemsList et comment vous avez ajouté des données dans la liste, veuillez partager
Essayez ceci dans le code Actvity / Fragment et non dans le code de l'adaptateur RecyclerView. mAdapter.notifyDataSetChanged ();
notificationItemsList est un ArrayList d'objets d'une autre classe NotificationItemm.
@AkashSureshDandwate j'ai ajouté le code de mon constructeur dans le post
Je dis que comment vous avez ajouté ces données à la liste
Pour contourner ce problème, vous pouvez appeler une méthode dans FragmentClass, qui charge la nouvelle liste (avec l'élément supprimé) dans votre adaptateur. Appelez cette méthode depuis votre adaptateur
public void MethodInFragmentClass(NotificationItemm todelete)
{
/*
... delete item
*/
notificationsTabAdapter = new NotificationsTabAdapter(getContext(), R.id.notificationsRecyclerView,
notificationDeletedItemsList, cListner);
recyclerView.setAdapter(notificationsTabAdapter);
}
Essayez ceci, l’espoir fonctionnera pour vous.
notificationItemHolder.imageDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notificationItems.remove(position);
notifyDataSetChanged();
}
Peut-être un problème avec l'objet item (il peut en faire référence à un objet différent). Pouvez-vous publier votre classe d'adaptateur complète? afin que nous puissions vous aider
Dans votre NotificationsTabAdapter , effectuez les modifications suivantes
@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) {
//perform normal remove operation here
notificationItems.remove(position);
notificationsTabAdapterListener.onItemsDeleted(position);
}
});
et implémentez NotificationsTabAdapterListener dans votre fragment et dans la méthode de remplacement, utilisez le code suivant
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;
Je l'ai fait, et maintenant je reçois une exception de point nul sur le rappel onNotificationItemDelete dans l'interface
avez-vous implémenté NotificationsTabAdapterListener dans votre fragment?
et l'a passé dans le constructeur NotificationsTabAdapter
Oui je l'ai, mais quand même
changement de yourAdaptor.notifyDataSetChanged (); aux notificationsTabAdapter.notifyDataSetChanged (); dans le rappel onNotificationItemDelete
j'ai déjà ajouté des notifications Adapter.notifyDataSetChanged (); au rappel
notificationsTabAdapter = new NotificationsTabAdapter (getContext (), R.id.notificationsRecyclerView, notificationItemsList, this); - créez-vous votre adaptateur comme ça?
non. J'ai déjà ajouté le code de la classe de fragment où l'adaptateur est créé.
oui vous passez cListner au lieu de ceci
Oui, oui, compris
Utilisez un rappel comme celui-ci dans votre adaptateur:
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();
}
}
Et dans votre notification notifydatasetchangez et mettez à jour recyclerview comme ceci: p>
private ICallback mICallback;
public interface ICallback {
void deleteItem(int position);
}
public SettingRecyclerViewAdapter(SettingMediator settingMediator, ICallback ICallback) {
mICallback = ICallback;
mSettingMediator = settingMediator;
}
Essayez ceci:
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();
}
});
Vous devez analyser la position qui va de l'écouteur onclick dans la classe Adapter à la méthode Interface (onitemClicked ()) Ensuite, implémentez la classe d'interface dans la classe Fragment et supprimera la position que nous avons sur la méthode Interface en utilisant [listname.remove (position))] Éventuellement, mettez à jour l'interface utilisateur de recyclage à l'aide de adaptername.notifyDataSetChanged ();
Étape 1: créer une classe d'interface
@Override
public void onitemClicked(View v, int position) {
Listname.remove(Listname.get(position));
RecyclerviewAdaptername.notifyDataSetChanged();
}
Étape 2: passer la position à la méthode d'interface dans la classe d'adaptateur p>
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);
}
});
étape 3: implémenter la classe et la méthode d'interface dans le fragment
public interface RecyclerviewItemClickListener {
void onitemClicked(View v, int position);
}
oui même j'ai eu le même problème
@ bk7 avez-vous trouvé une solution pour cela?
oui je l'ai fait après quelques recherches
partager votre code constructeur NotificationsTabAdapter je publierai la solution
Je pense que vous devez définir notifydatasetchanged () dans votre fragment avec rappel et définir à nouveau l'adaptateur sur votre recyclerview.
Veuillez poster votre constructeur d'adaptateur
Cela peut être dû aux paramètres du constructeur de l'adaptateur
J'ai ajouté le code constructeur, veuillez vérifier
J'ai ajouté la réponse s'il vous plaît vérifier @Lenzman
Sur la notification de suppression, cliquez sur coller à
deleteNotificationsur la position d'un élément en tant qu'argument. Ensuite, supprimez l'élément en utilisant la position de notificationItemsList et notifiez. La prochaine fois, téléchargez le code complet. Ne perdez pas de temps aux autres.essayez d'ajouter ce
notificationsTabAdapter.notifyDataSetChanged ()aprèssetAdapter ()