简体   繁体   中英

RecyclerView Item position does not get update

In the following code, I could able to delete the image via clicking imgBtn, but my images does not scroll the deleted image position, it just stays its current position, it does not update.

Main Activity Class

ItemData itemsData[] = {new ItemData("Help", R.drawable.image),
            new ItemData("Delete", R.drawable.orange),
            new ItemData("Cloud", R.drawable.image),
            new ItemData("Favorite", R.drawable.orange),
            new ItemData("Like", R.drawable.image),
            new ItemData("Rating", R.drawable.orange)};

 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
 recyclerView.setHasFixedSize(true);
 LinearLayoutManager llm = new LinearLayoutManager(this);
 llm.setOrientation(LinearLayoutManager.HORIZONTAL);
 recyclerView.setLayoutManager(llm);
 CustomAdapter mAdapter = new CustomAdapter(itemsData);
 recyclerView.setAdapter(mAdapter);

ItemData Class

public class ItemData {
private String title;
private int imageUrl;

public ItemData(String title,int imageUrl){

    this.title = title;
    this.imageUrl = imageUrl;
}
String getTitle(){return title;}
void setTitle(String t){title = t;}
int getImageUrl(){return imageUrl;}
void setImageUrl(int t){imageUrl = t;}
}

Custom Adapter Class

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder>
{
    private ItemData[] itemsData;
    private Context context;
    private CustomAdapter thisAdapter = this;

    public CustomAdapter( ItemData[] itemsData) {
        this.itemsData = itemsData;
    }


@Override
public void onBindViewHolder(ViewHolder viewHolder, int pos) {
    final int position = pos;  

    viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
    viewHolder.imgBtn.setImageResource(R.drawable.ic_close_black_24dp);

    viewHolder.imgBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((FrameLayout) v.getParent()).removeAllViews();
        }
    });
 }

}

You should avoid manually modifying the views when using a RecyclerView. Use the adapters notify* methods to push any updates to the list view. In your specific case I'd suggest using a List<ItemData> instead of an array and then in your onClickListener code I'd do this:

@Override
public void onClick(View v) {
    itemsData.remove(position);
    notifyItemRemoved(position);
}

I can't exactly tell how the data is used to populate your RecyclerView, I'm assuming it is itemsData, but you'd remove the item in question from it and then issue a notifyItemRemoved(position) call to that adapter.

If you want to remove Item from the recylerview. dont do it like this.

Because you using ItemData[] array

List<ItemData> numlist = new ArrayList<ItemData>();
for(int i= 0;i<itemsData.length;i++)
{
    if(i == position)
    {
        // No operation here 
    }
    else
    {
        numlist.add(arr_fav[i]);
    }
}
itemsData = numlist.toArray(new String[numlist .size()]);
notifyItemRemoved(position);
notifyItemRangeChanged(0, mDataset.size());

If you convert it to List<ItemData> Go with asadmshas's Answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM