为什么recyclerView总是删除最后一项

时间:2015-10-04 13:21:36

标签: android android-recyclerview recycler-adapter

这里我在recyclerview中添加和删除editText中的项目。添加工作正常,但是当我删除arraylist中的任何项目时,删除了正确的项目,但recyclerview始终删除最后一项。 这是代码

public static class ViewHolder extends RecyclerView.ViewHolder {
        EditText listItemEditText;
        TextView addNew;
        ImageView removeItem;
        public ViewHolder(View view) {
            super(view);
            listItemEditText = (EditText)view.findViewById(R.id.list_item);
            addNew = (TextView)view.findViewById(R.id.add);
            removeItem = (ImageView)view.findViewById(R.id.deleteItem);
        }
    }

    @Override
    public int getItemCount() {
        return listItems.size();    
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.listItemEditText.setId(position);
        final TextWatcher textWatcher = new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


                try{
                    listItems.set(position,s.toString());
                }catch(Exception e){
                    listItems.add(position,s.toString());
                }
                if(s.length() >0 && !edited.get(position)){
                    edited.set(position, true);
                    holder.addNew.setVisibility(View.VISIBLE);
                }else if(s.length()==0){
                    holder.addNew.setVisibility(View.GONE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        };
        holder.listItemEditText.addTextChangedListener(textWatcher);
        holder.addNew.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                edited.add(position,true);
                edited.add(position+1,false);
                addEmptyRow(position+1);
                v.setVisibility(View.GONE);
            }
        });
        holder.removeItem.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                deleteRow(position,holder);
            }
        });
    }
    protected void addEmptyRow(int i) {
        listItems.add(i,"");
        notifyItemInserted(i);
    }

    @Override
    public NoteListItemsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
        View v = LayoutInflater.from(MainActivity.activity).inflate(R.layout.list_notes, parent,false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    private void deleteRow(int position, ViewHolder holder){
        Toast.makeText(MainActivity.activity, "position="+position+", Size="+listItems.size(), Toast.LENGTH_SHORT).show();
        listItems.remove(position);
        for(String ass:listItems){
            System.out.println(ass);
        }
        edited.remove(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();

    }

1 个答案:

答案 0 :(得分:2)

这是错误的:

public void onBindViewHolder(final ViewHolder holder, final int position)

应该是:

public void onBindViewHolder(final ViewHolder holder, int position)

换句话说,你无法进行position决赛,因为当你添加项目时,位置会发生变化,但RecyclerView不会因为位置发生变化而重新绑定现有项目(因此最终位置会变得过时)。

相反,请在点击内部使用viewHolder.getAdapterPosition()以获取最新位置。

此外,通过在onBind中执行此操作,您将创建不必要的侦听器对象,这对内存利用率有害(更多GC)。

这样实现它:

@Override
public NoteListItemsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
    View v = LayoutInflater.from(MainActivity.activity).inflate(R.layout.list_notes, parent,false);
    final ViewHolder holder = new ViewHolder(v);
    holder.removeItem.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = holder.getAdapterPosition();
            if (pos != RecyclerView.NO_POSITION) {
                deleteRow(position,holder);
            }
        }
    });
    // do the same for all listeners
    return holder;
}

这样做效果很好,你可以避免搅动物体。