删除recyclerView中的所有选定项目

时间:2018-06-25 14:34:14

标签: java android android-recyclerview

我正在尝试从recyclerView中删除多个项目。 recyclerView中的项目具有属性isSelected。当用户单击recyclerView项时,我将属性isSelected设置为true。选择后,用户可以单击选项菜单中的delete按钮删除所选项目。该代码具有异常行为,例如,当按下delete按钮时,某些选定的项目将被删除,而有些则未被删除。另外,某些项目会在随机位置自动选择。

Model.java

public class Model {
    private String text;
    private boolean isSelected = false;

    public Model(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }


    public boolean isSelected() {
        return isSelected;
    }
}

RecyclerViewAdapter.java

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

    private List<Model> mModelList;
    private Context mCtx;

    public RecyclerViewAdapter(Context ctx, List<Model> modelList) {
        this.mModelList = modelList;
        this.mCtx = ctx;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.item_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final Model model = mModelList.get(position);
        holder.view.setBackgroundColor(model.isSelected() ? Color.CYAN : Color.WHITE);
        holder.tvItems.setText(model.getText());
        holder.tvItems.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                model.setSelected(!model.isSelected());
                holder.tvItems.setBackgroundColor(model.isSelected() ? Color.CYAN : Color.WHITE);
                notifyItemChanged(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mModelList == null ? 0 : mModelList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvItems;
        private View view;

        public MyViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            tvItems = itemView.findViewById(R.id.tvItems);
        }
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private List<Model> mModelList;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;

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

        mRecyclerView = (RecyclerView) findViewById(R.id.rvListItems);
        mAdapter = new RecyclerViewAdapter(this, getListData());
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
    }

    private List<Model> getListData() {
        mModelList = new ArrayList<>();
        for (int i = 1; i <= 25; i++) {
            mModelList.add(new Model("TextView " + i));
        }
        return mModelList;
    }


    private void deleteSelectedItems() {
        if (mModelList != null){
            for (int i=0; i<mModelList.size(); i++){
                if (mModelList.get(i).isSelected()){
                    Log.d("testingTAG", String.valueOf(i));
                    mModelList.remove(i);
                    mAdapter.notifyItemRemoved(i);
                    mAdapter.notifyItemRangeChanged(i, mModelList.size());
                    i--;
                }
            }
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_delete:
                deleteSelectedItems();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您应在clicklistener内使用holder.getAdapterPosition()。按如下所示更改onclick并尝试。

 $('.slider').data('container')
 $('.slider').data('speed')
 $('.slider').data('container')
 //...

答案 1 :(得分:0)

来自@pop的答案将选择项目,如果选择了项目,则将背景色更改为CYAN,然后将Model类实例的选择标记反向。

您应该使用迭代器从mModelList中删除所选项目,不能从for循环中的列表中删除项目

Calling remove in foreach loop in Java