然而,当我使用notifyItemChanged(position);时,仅在按以下顺序的复选框时才会正确删除项目:1,2,3,4,5或5,4,3,2,1。如果我想要删除第一个项目,它会将其删除,但是如果我想要删除第三个项目,则会删除第四个。
我发现的另一个选项是使用notifyDataSetChanged();并且它可以按任何顺序工作,但是那么没有显示动画。
我希望在添加和删除项目时有动画效果,因此如果有一种方法可以使用notifyDataSetChanged();实现这一点,我需要帮助。
public class ListAdapter extends RecyclerView.Adapter{
SharedPreferences sharedpreferences, prefs;
public static final String MyPREFERENCES = "MyPrefs";
public static final String date = "date";
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.nd_table, parent, false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int adapterPosition) {
//((ListViewHolder) holder).bindView(position);
String data = hwName.get(adapterPosition);
String data2 = hwDesc.get(adapterPosition);
((ListViewHolder) holder).mItemText.setText(data);
((ListViewHolder) holder).mItemText2.setText(data2);
((ListViewHolder) holder).buttonDelete.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
removeItem(adapterPosition);
notifyItemChanged(adapterPosition);
}
});
}
@Override
public int getItemCount() {
return hwName.size();
}
public class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mItemText, mItemText2;
public CheckBox buttonDelete;
public ListViewHolder(View itemView) {
super(itemView);
mItemText = (TextView) itemView.findViewById(R.id.pam_name);
mItemText2 = (TextView) itemView.findViewById(R.id.pam_nd);
buttonDelete = (CheckBox) itemView.findViewById(R.id.nd_checkbox);
itemView.setOnClickListener(this);
}
public void bindView(int position) {
String value = hwName.get(position);
String value2 = hwDesc.get(position);
mItemText.setText(value);
mItemText2.setText(value2);
//notifyItemInserted(hwName.size());
}
public void onClick(View view) {
}
}
public void removeItem(int position){
hwName.remove(position);
hwDesc.remove(position);
//notifyDataSetChanged();
notifyItemRemoved(position);
notifyItemChanged(position);
}}
崩溃报告:
注:此报告显示应用程序在执行过程中遇到了一个问题,导致程序崩溃。具体问题是由于数组越界所致,即尝试访问不存在的数组元素。致命异常:主线程 进程:com.sajev.slush,PID:14166 java.lang.IndexOutOfBoundsException: 索引:5,大小:5 at java.util.ArrayList.remove(ArrayList.java:477) at com.sajev.slush.ListAdapter.removeItem(ListAdapter.java:89) at com.sajev.slush.ListAdapter$1.onClick(ListAdapter.java:45) at android.view.View.performClick(View.java:5646) at android.widget.CompoundButton.performClick(CompoundButton.java:123) at android.view.View$PerformClick.run(View.java:22459) at android.os.Handler.handleCallback(Handler.java:761) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)