我有ArrayList
< String>有6个项目{“1”,“2”,“3”,“4”,“5”,“6”}。
我用CardView
代表这些项目。我想要向左滑动删除项目。
看起来像这样:http://imgur.com/cuWoiB3
当我从数字2向右滑动时,它看起来像这样:http://imgur.com/5E9fwP0
如果我删除了数字5,它将删除数字2.如果我删除数字3,它将删除数字1.它似乎是完全随机的。
我在这里遗漏了什么吗?我该如何解决这个问题?
相关代码:
我的适配器:
public class ChoresAdapter extends RecyclerView.Adapter<ChoresAdapter.ChoreViewHolder>{
private ArrayList<String> chores;
public ChoresAdapter(ArrayList<String> chores){
this.chores = chores;
}
@Override
public ChoreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cardview, parent, false);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
ChoreViewHolder pvh = new ChoreViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(ChoreViewHolder holder, int position) {
holder.choreName.setText(this.chores.get(position));
}
@Override
public int getItemCount() {
return this.chores.size();
}
public void removeAt(int position) {
this.chores.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
}
public static class ChoreViewHolder extends RecyclerView.ViewHolder {
CardView cv;
public static TextView choreName;
public static TextView personAge;
public static ImageView personPhoto;
ChoreViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
choreName = (TextView)itemView.findViewById(R.id.chore_name);
}
}
}
我片段中的相关代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chores, container, false);
RecyclerView rv = (RecyclerView)view.findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
chores = new ArrayList<>();
chores.add("1");
chores.add("2");
chores.add("3");
chores.add("4");
chores.add("5");
chores.add("6");
adapter = new ChoresAdapter(chores);
SwipeableRecyclerViewTouchListener swipeTouchListener =
new SwipeableRecyclerViewTouchListener(rv,
new SwipeableRecyclerViewTouchListener.SwipeListener() {
@Override
public boolean canSwipe(int position) {
return true;
}
@Override
public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
adapter.removeAt(position);
}
}
@Override
public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
adapter.removeAt(position);
}
}
});
rv.addOnItemTouchListener(swipeTouchListener);
rv.setAdapter(adapter);
return view;
}
答案 0 :(得分:1)
有点晚了,我只是遇到了同样的情况并得到了答案,只需使用
chores.remove(position);
adapter.notifyDataSetChanged();
您不需要在适配器类上执行此操作。
答案 1 :(得分:0)
您可以使用以下代码(Android支持V7中的ItemTouchHelper.SimpleCallback
)使用滑动手势从RecyclerView中删除卡片。从RecyclerView中滑动时,您还需要从ArrayList中删除元素。
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
chores.remove(viewHolder.getAdapterPosition());
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
@Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recList);