問題が発生しました。RecyclerViewでアイテムを削除できませんでした。インターネットで検索しようとしていますが、この投稿に従ってフォローしている解決策が見つかりませんでした Android RecyclerViewの追加とアイテムの削除 それでも選択したアイテムを削除できませんでした以下はアダプターのコードですと断片
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
}
Adapterクラス内のアイテムを削除できませんでした。誰かが私を助けることができますか?
削除の正しい位置を取得するには、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());
}
リストから「豚」を削除します。
int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);
リストから「ラクダ」と「羊」を削除します。
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);
リスト全体をクリアします。
data.clear();
adapter.notifyDataSetChanged();