-1

I want to remove a specific item (FIRST ITEM!) from the recyclerview.

its not a problem:

//items variable is my Arraylist
items.remove(0);

Its working okay, when i see the item. But when I scroll, the recyclerview updates itself, and if I scroll then try to remove the item its not the first item (0). What is the code, to remove the first item in ANY case?

Thank you!

Janos
  • 177
  • 3
  • 15
  • 2
    You need to update arraylist which you have used in your adapter, and also needs to refresh `RecyclerView` – Ravi Nov 03 '16 at 13:21
  • can you explain it more, to how to do that? – Janos Nov 03 '16 at 13:24
  • check [this](http://stackoverflow.com/questions/31367599/how-to-update-recyclerview-adapter-data) – Ravi Nov 03 '16 at 13:32
  • i dont have problem with deleting an item, the problem is its not the right item. when i scroll, the first item wont be the first item.. how to tell the adapter, that i need to remove the first item? – Janos Nov 03 '16 at 13:57

2 Answers2

0
private List<Item> mItems;
private MyAdapter mAdapter;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // ... On Bind
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    // View holder class to save view elements
    class ViewHolder extends RecyclerView.ViewHolder {
        // ...
        public ViewHolder(View itemView) {
            super(itemView);
            // ...
        }
    }
}

private void removeFirstItem() {
    mItems.remove(0);
    mAdapter.notifyDataSetChanged();
}

Or use notifyItemRemoved(position) to remove with animation.

Guntars Ļuta
  • 61
  • 2
  • 10
0

I think you can read the answers in this question Get visible items in RecyclerView.

Besides you can not call notifyItemRemoved and other notify method before activity is created, or you will get an error "Cannot call this method while RecyclerView is computing a layout or scrolling"

Community
  • 1
  • 1
Harlan
  • 779
  • 4
  • 15