我正面临一个问题,我无法删除RecycerView中的一个项目。我试图在互联网上搜索,但无法看到任何解决方案,我遵循这篇文章Android Recycerview添加和删除项目,但我仍然无法删除下面选择的项目是我的适配器和碎片的代码
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();
}
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
要获得删除的正确位置,您应该调用holder.getAdapterPosition()
并将此值用于所有删除和通知操作。Android studio对此显示了一个警告,所以一定要先检查警告。为什么对于getitemcount
返回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());
}
很管用。然而,我也需要执行一些撤消操作或确认。做这件事最好的方法是什么? 第一个问题是如何插入另一个视图代替删除与确认对话框?如果用户选择撤消删除,如何恢复被刷的项目?
但是建议的解决方案对我来说都不起作用。我从sqlite数据库中的RecycerView列表项,当我向左滑动时,相应的数据将成功地从数据库中删除。由于某些原因,昨天一切都工作得很好,删除的项从列表中消失了,但从今天开始,该项在RecycerView中仍然可见。下面是我的代码:
我正在将我的代码从使用< code>androidx.viewpager更新为< code>androidx.viewpager2。我正在翻阅一个不确定数量的片段,这些片段显示了从数据库中检索到的数据记录。加载视图分页器并对我的数据进行分页运行得很好,但是我在删除项目和更新分页器适配器时遇到了一些问题。我想通过调用我的适配器上的< code>removeItem()方法(参见下面的代码)在任何给定
任何努力都将受到赞赏。