我将展示代码并在解决问题的步骤之后.
我在一个标签片段中有一个recyclerview,它从自定义对象中获取数据集:
mRecyclerView = (RecyclerView) v.findViewById(R.id.recyclerview); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerAdapter = new MyRecyclerAdapter(mMes.getListaItens(),this,getActivity()); mRecyclerView.setAdapter(mRecyclerAdapter);
我在适配器的onBindViewHolder()中设置列表项的longclick行为:
@Override public void onBindViewHolder(final ViewHolder holder,final int position) { ItemMes item = mListaItens.get((position)); holder.descricao.setText(item.getDescrição()); holder.valor.setText(MainActivity.decimalFormatWithCod.format(item.getValor())); ... holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { new MaterialDialog.Builder(mContext) .title(holder.descricao.getText().toString()) .items(R.array.opcoes_longclick_item) .itemsCallbackSingleChoice(-1,new MaterialDialog.ListCallbackSingleChoice() { @Override public boolean onSelection(MaterialDialog dialog,View view,int which,CharSequence text) { switch (which) { case 0: mParentFragment.showUpdateItemDialog(position); return true; case 1: mParentFragment.showDeleteItemDialog(position); return true; } return false; } }) .show(); return true; } }); }
public void showDeleteItemDialog(int position) { final ItemMes item = mMes.getListaItens().get(position); new MaterialDialog.Builder(getActivity()) .title("Confirmar Remoção") .content("Tem certeza que deseja remover " + item.getDescrição() + "?") .positiveText("Sim") .negativeText("Cancelar") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog,@NonNull DialogAction which) { deleteItem(item); } }) .show(); } public void deleteItem(ItemMes item) { getMainActivity().deleteItemFromDatabase(item.getID()); int position = mMes.getListaItens().indexOf(item); mMes.getListaItens().remove(position); mRecyclerAdapter.notifyItemRemoved(position); atualizaFragment(); }
public int deleteItemFromDatabase(long id) { sqliteDatabase db = dataBaseHelper.getWritableDatabase(); String where = DBHelper.COLUNA_ID + " = ?"; String[] args = {String.valueOf(id)}; int rowsAffected = db.delete(DBHelper.TABELA_ITEM,where,args); db.close(); return rowsAffected; }
现在我将重现这些步骤:
我在listview中显示3个itens.然后我尝试删除第一个:
1 – 通过正确的索引拦截longclick:
3 – 完成所有这些之后,正如预期的那样,适配器正在存储并显示2个项目……
所以,如果我尝试删除这个2项列表的第一项,我得到错误的位置(应该是0,是1):
而且,如果我尝试删除这两个项目列表的最后一项,我得到错误的位置(应该是1,是2):
问题是:如果我有一个大小为2的数据集(并且适配器知道它),它如何调用onBindViewHolder(ViewHolder holder,int [last index 1])?
我不知道会出现什么问题.所以我请求帮助,因为我正在考虑放弃这个项目,因为我做的一切都是正确的,但总是有些东西不起作用,我累了.
提前致谢.
解决方法
我注意到在方法onBindViewHolder(VH holder,int position)中,当位置出错时,holder.getAdapterPosition()总是给我正确的位置.
所以我改变了我的代码:
ItemMes item = mListaItens.get((position)); ... mParentFragment.showUpdateItemDialog(position); ... mParentFragment.showDeleteItemDialog(position); ....
至:
ItemMes item = mListaItens.get((holder.getAdapterPosition())); ... mParentFragment.showUpdateItemDialog(holder.getAdapterPosition()); ... mParentFragment.showDeleteItemDialog(holder.getAdapterPosition()); ....
一切都运作良好.这很奇怪,但……谢谢大家.