I have a delete button in each item of RecyclerView. It works fine but the view below RecyclerView doesn't follow up.
I try to follow Android RecyclerView addition & removal of items but it still not works
Here my code
public ViewHolder(View itemView) {
super(itemView);
btnDelete = (ImageButton)itemView.findViewById(R.id.detail_delete);
btnDelete.setOnClickListener(this);
}
#Override
public void onClick(View v) {
removeAt(getAdapterPosition());
}
private void removeAt(int position) {
scheduleList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, scheduleList.size());
}
I think the problem lies here...
notifyItemRangeChanged(position, scheduleList.size());
you're technically lying to the RecyclerView and the adapter by saying you've removed all items located after the specified position...that's what the last parameter is for...number of items to remove which is incorrect because you are passing in the size of the whole list. This what the last parameter stands for...from the javadocs...
itemCount - Number of items removed from the dataset
Basically, you will need to remove that line in the removeAt method so that you only notify the observers once...
private void removeAt(int position) {
scheduleList.remove(position);
notifyItemRemoved(position);
}
if it still doesn't work, then you might be having yet another problem somewhere else
Add the function notifyDataSetChanged(). I was facing the same problem
#Override
public void onClick(View v) {
removeAt(getAdapterPosition());
adapter.notifyDataSetChanged(); //adapter is your card adapter
}
Related
In my app I have a bottom navigation including home and favorites. in the recyclerview when I click on like button it'll be added to favourites. now when clicked again, I want to remove it from favorite at the moment.
In my code the item is deleted but it won't be removed until I go to home and return.
Note that I can't use arraylist.remove(position) because if so, even in home fragment the item is removed and I don't want that.
code in adapter:
itemHolder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int rowId = databaseHelper.GetId((String) itemHolder.headline.getText().toString());
if (databaseHelper.IsLiked(rowId)){
databaseHelper.UpdateLiked(rowId, false);
notifyItemRemoved(position);
notifyItemChanged(position);
}
else{
databaseHelper.UpdateLiked(rowId, true);
notifyDataSetChanged();
notifyItemChanged(position);
}
}
yourarraylist.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, yourarraylist.size());
I have a recyclerview. In its item layout , I have a plus button . If user click on that button it will become minus and new layout will added below it . If user click on minus it will remove from recyclerview as well as from the dataset .
Problem is :
When user click minus it removes properly from both recyclerview and dataset but on adding new layout it appears with old data again . I make sure that it deletes from dataset but still appears again.
My Adapter Code:
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.img_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (if it is plus) {
holder.img_delete.setImageResource(set minus drawable);
MyModel model = new MyModel();
model.setPrice("");
data.add(model);
notifyItemInserted(position+1); //+1 Because first time im adding one data manually in onCreate to set adapter
} else {
data.remove(position);
notifyItemRemoved(position);
}
}
});
I have a list of elements in recycler view, my need is to hide some elements by clicking a specific element of the same list. For instance, my list contains 10 elements and I want to hide all the elements from position 6 by clicking the element in the 5th position. how can I do that?
You have to remove that element from dataset and call notifyOnDataSetChanged Method, otherwise you can create a model class with boolean/hide show flag and then on item click listener you can set flag accordingly to remove/hide element.
Recyclerview react to notifyDatasetChanged method to redraw each visible row.
try to change behind model of recycler view then notifyDatasetChanged
bindViewHolder(VH holder, int position){
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do you business to change data model in other position which
//identified whether the view must be visible or not
notifyDataSetChanged();
}
});
}
Just try this...
public void onBindViewHolder(final ViewHolder viewHolder,
final int position) {
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// set your logic like this..
if(position==5){
if(list.size()>6){
list.remove(6); // here list will be your data list.
notifyDataSetChanged();
}
}
}
});
I got a RecyclerView and want to change the appearance of any clicked row. For that I have a callbackFunction in my Activity which I pass to the Adapter, which then is called inside the Adapter, as soon as I click on any row in the RecyclerView.
The clicked row is then changed, but it happens, that not only the clicked rows are changed but also other rows, that weren't clicked and were never clicked before. I checked the ArrayList that contains the data, but everything is fine there. Only the clicked elements contain the trigger to change the appearance of the row.
What is causing the other rows to change, although they have not been clicked?
Interface inside activity for callback
public interface onHeaderClickListener{
void onHeaderClicked(int index);
}
Inside RecyclerView Adapter
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderHeader){
((ViewHolderHeader)holder).dateHeaderTextView.setText( Integer.toString(((objClass_offerDateHeader) arrayList.get(position)).getDate()));
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
}
((ViewHolderHeader)holder).dateHeaderTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onHeaderClickListener.onHeaderClicked(position);
}
});
}
}
Adapter initialisation inside activity
customAdapterRecyclerViewAddOffersTo = new customAdapterRecyclerViewAddOffers(offerArrayList,"dragTo", new onHeaderClickListener() {
#Override
public void onHeaderClicked(int index) {
if (offerArrayList.get(index) instanceof objClass_offerDateHeader){
if(((objClass_offerDateHeader) offerArrayList.get(index)).isSelected()){
((objClass_offerDateHeader) offerArrayList.get(index)).setSelected(false);
}
else {
((objClass_offerDateHeader) offerArrayList.get(index)).setSelected(true);
}
customAdapterRecyclerViewAddOffersTo.notifyDataSetChanged();
}
}
});
In your onBindViewHolder method you have to set the background of the unselected cell, keep in mind the the cells are reused and you only set the background of selected cells so when it is reused the background is not returned to the normal color
So in code you will have to add an else condition
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
} else {
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#FFFFFF")); // I assume you need it to be white you can change it to any other color
}
You need to add an else condition here:
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
}
Viewholders get recycled, so you cannot be sure of the current state when onBindViewHolder is called.
my RecyclerView contains Text and Delete Button when delete is clicked specific row will be deleted from ArrayList
pending.remove(p);// pending is arraylist
after this updating RecyclerView by calling this function which is inside Adapter
swap(pending);
public void swap(ArrayList<DownloadingActivity.scheduleListType> newList) {
if (pendingList != null) {
pendingList.clear();
pendingList.addAll(newList);
} else {
pendingList = newList;
}
notifyDataSetChanged();
}
but the problem is that correct item deleted successfully from ArrayList pending but on RecyclerView wrong update is displayed.
if i delete second item from array list pending but in RecyclerView updated
deleted item is last
when activity is reloaded then values in RecyclerView are showing correctly
searched lot of about it but did not found any way to solve
Instaead of using swap() to delete the item from your array list and adding them back into diffrent array list, I will suggest that you use notifyItemRemoved in your adapter. So, your adapter will look like below.
RecyclerViewAdapter.java :
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
DownloadingActivity.scheduleListType> p = pendingList.get(position);
//....
//....
holder.deleteBnt.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
pendingList.remove(p);
notifyItemRemoved(position)
}
})
}