为什么RecyclerView会添加最后删除的CardView?

时间:2017-07-31 23:28:06

标签: java android android-recyclerview android-cardview recycler-adapter

所以我有一个包含CardView的RecyclerView,我的Activity中有一个按钮,每次点击它都会向RecyclerView添加一张新卡。

在每张卡片中,我有另一个按钮,当按下该按钮时,删除被触摸的CardView。

问题是我向RecyclerView添加了新行,我删除了一些,然后当我再次添加行时添加的新行是我之前删除的旧行。

我该如何解决这个问题?

这是我的活动:

public class MainActivity extends AppCompatActivity {

    private ArrayList exercises;
    private Button btnNewExercise;
    private RecyclerView recyclerView;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        exercises = new ArrayList<>();

        final RecyclerView.Adapter adapter = new RecyclerViewAdapter(exercises);
        recyclerView.setAdapter(adapter);

        btnNewExercise = (Button) findViewById(R.id.btnNewExercise);


        btnNewExercise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exercises.add("Test" +i);
                i++;

                adapter.notifyDataSetChanged();
                adapter.notifyItemInserted(exercises.size());
            }
        });


    }
}

这是我的适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private ArrayList<String> mDataset;


    public RecyclerViewAdapter(ArrayList myDataset) {
        mDataset = myDataset;
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view_row, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerViewAdapter.ViewHolder viewHolder, int i) {
        viewHolder.tv_exercise.setText(mDataset.get(i));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_exercise;
        private Button btnEliminarRow;

        public ViewHolder(View view) {
            super(view);
            tv_exercise = (TextView)view.findViewById(R.id.exercise_text);
            btnEliminarRow = (Button) view.findViewById(R.id.btnEliminarRow);
            final View posas = view;


            btnEliminarRow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    removeAt(getAdapterPosition());
                }
            });
        }

        public void removeAt(int position) {
            mDataset.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, mDataset.size());
        }
    }
}

添加3个元素时记录:

07-31 19:02:57.643 21770-21770/com.ravelinx.prueba I/System.out: Test0
07-31 19:02:58.070 21770-21770/com.ravelinx.prueba I/System.out: Test0
07-31 19:02:58.128 21770-21770/com.ravelinx.prueba I/System.out: Test1
07-31 19:02:58.505 21770-21770/com.ravelinx.prueba I/System.out: Test0
07-31 19:02:58.536 21770-21770/com.ravelinx.prueba I/System.out: Test1
07-31 19:02:58.569 21770-21770/com.ravelinx.prueba I/System.out: Test2

删除2个元素时没有日志

然后再次添加2个元素时记录:

07-31 19:04:24.426 22679-22679/com.ravelinx.prueba I/System.out: Test0
07-31 19:04:24.467 22679-22679/com.ravelinx.prueba I/System.out: Test3
07-31 19:04:24.727 22679-22679/com.ravelinx.prueba I/System.out: Test0
07-31 19:04:24.769 22679-22679/com.ravelinx.prueba I/System.out: Test3
07-31 19:04:24.787 22679-22679/com.ravelinx.prueba I/System.out: Test4

以下是显示此问题的视频:

https://www.youtube.com/watch?v=TgFomjUJq8c

1 个答案:

答案 0 :(得分:0)

更新setHasFixedSize(true)上导致整个问题导致现有代码库本身删除此行应该可行。

addItem()这样的内容添加到RecyclerViewAdapter.java

public void addItem(String item, int position) {
    this.mDataset.add(position, item);
    notifyItemInserted(position);
}

并替换 btnNewExercise.setOnClickListener()的正文,如下所示

{
   exercises.add("Test" +i);
   i++;
   adapter.addItem("test", adapter.getCount());
}