Remove an item in RecyclerView- Android

假如想象 提交于 2019-12-07 04:26:50

问题


I am facing a problem, I could not remove an Item in RecyclerView. I am trying to search in Internet, but could not see any solution I am following to this post Android RecyclerView addition & removal of items But I still could not remove the item selected Below is my code for my adapter and fragement

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
List<VideoDetails> contents2;
Context mContext;
ImageLoader imageLoader;
static final int TYPE_HEADER = 0;
static final int TYPE_CELL = 1;
public RecyclerViewAdapter(List<VideoDetails> contents) {
    this.contents2 = contents;
}
public RecyclerViewAdapter(Context context, List<VideoDetails> contents) {
    mContext = context;
    this.contents2 = contents;
    imageLoader = new ImageLoader(context);
}
@Override
public int getItemViewType(int position) {
    return position;

}
@Override
public int getItemCount() {
    return contents2.size();
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    View view = null;
    view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_card_small2, parent, false);
    ViewHolder vh = new ViewHolder(view);
    return vh;
}
 @Override
public void onBindViewHolder(ViewHolder holder,final int position) {
    holder.title.setText(contents2.get(position).getTitle());
     holder.image.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View view) {
             Log.d("thien.van","on Click onBindViewHolder");
             contents2.remove(position);
             notifyItemRemoved(position);
             notifyItemRangeChanged(position, contents2.size());

         }
     });
}
public Bitmap getThumbnail(String str) {

    Bitmap bMap = ThumbnailUtils.createVideoThumbnail(str, MediaStore.Video.Thumbnails.MINI_KIND);
    return bMap;
}
public class ViewHolder extends RecyclerView.ViewHolder implements  View.OnClickListener {


    public TextView title;
    public ImageView image;
    public ProgressBar progressBar;

    public ViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.title);
        image = (ImageView) itemView.findViewById(R.id.thumbnail);
        progressBar = (ProgressBar) itemView.findViewById(R.id.progress);
      //  image.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        Log.d("thien.van","onClick getAdapterPosition             () = "+getAdapterPosition());
        removeAt(getAdapterPosition());
    }
}
public void removeAt(int position) {
    contents2.remove(position);
    notifyDataSetChanged();
}

}

RecyclerViewFragment

public class RecyclerViewFragment extends Fragment {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;

private static final int ITEM_COUNT = 2;

private List<Object> mContentItems = new ArrayList<>();
private static List<VideoDetails> mContentItems2 = new ArrayList<>();

public static RecyclerViewFragment newInstance() {
    return new RecyclerViewFragment();
}

public static RecyclerViewFragment newInstance(List<VideoDetails> contentItems) {
    RecyclerViewFragment myFragment = new RecyclerViewFragment();
    //  mContentItems = contentItems;
    Bundle args = new Bundle();
    args.putSerializable("thienvan", (Serializable) contentItems);
    myFragment.setArguments(args);
    return myFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mContentItems2 = (List<VideoDetails>) getArguments().getSerializable("thienvan");
    // Log.d("thien.van","Fragment file name = "+mContentItems2.get(0).getTitle());
    return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(layoutManager);
    //mRecyclerView.setHasFixedSize(true);

    mAdapter = new RecyclerViewMaterialAdapter(new RecyclerViewAdapter(getActivity().getBaseContext(),mContentItems2));
    mRecyclerView.setAdapter(mAdapter);

    /*{
        for (int i = 0; i < ITEM_COUNT; ++i)
            mContentItems.add(new Object());
        mAdapter.notifyDataSetChanged();
    }*/

    MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}
//http://android--code.blogspot.com/2015/12/android-recyclerview-add-remove-item.html

}

I could not remove an item inside Adapter class. Anybody could help me ?


回答1:


To get the correct position for remove you should call holder.getAdapterPosition() and use this value for all remove and notify operations. Android studio shows a warning for this, so make sure to check the warnings first. And why for getItemCount you return 2?

public void onClick(View view) {
     int newPosition = holder.getAdapterPosition();
     Log.d("thien.van","on Click onBindViewHolder");
     contents2.remove(newPosition);
     notifyItemRemoved(newPosition);
     notifyItemRangeChanged(newPosition, contents2.size());

}



回答2:


Remove single item

Remove "Pig" from the list.

int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);

Remove multiple items

Remove "Camel" and "Sheep" from the list.

int startIndex = 2; // inclusive
int endIndex = 4;   // exclusive
int count = endIndex - startIndex; // 2 items will be removed
data.subList(startIndex, endIndex).clear();
adapter.notifyItemRangeRemoved(startIndex, count);

Remove all items

Clear the whole list.

data.clear();
adapter.notifyDataSetChanged();


来源:https://stackoverflow.com/questions/36858086/remove-an-item-in-recyclerview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!