如何从片段中的ReyclerView获取有关复选框的信息

时间:2016-07-25 11:17:13

标签: java android

我需要从recyclerView获取有关项目的信息。我尝试做这样的事情进入,但这没有工作,任何想法?我认为调用CheckBox是没用的,我不需要它。但现在我不知道该怎么做。

private void handlerForChannels() {
    list = ChannelsManager.getInstance().getChannelList();
    mAdapter = new SettingsCustomAdapter(context, posit, list);
    verticalGridView.setAdapter(mAdapter);
    textView.setText(R.string.title_channels);
    button.setText(R.string.select_all);
    final SparseBooleanArray mChecked = new SparseBooleanArray();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAdapter.getItemViewType(SETTINGS_CHANNELS);
            mAdapter.getItemId(getId());
            CheckBox cb;
            int count = list.size();
            cb = (CheckBox) view.findViewById(R.id.checkBox_for_recycle);
            for (int i = 0; i < count; i++) {
                mChecked.put(i, cb.isChecked());
            }


            mAdapter.notifyDataSetChanged();

        }
    });
    button.setNextFocusLeftId(R.id.select_all);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    button1.setText(R.string.cancel_text);
    button1.setNextFocusLeftId(R.id.select_all);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ((MainActivity) activity).closeSettingsDrawerFragment();
        }
    });
    button2.setText(R.string.ok_text);
    button2.setNextFocusLeftId(R.id.ok_button);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

2 个答案:

答案 0 :(得分:3)

使用布尔值将新数组添加到适配器中,以设置复选框true或false:

    //...
    public ArrayList<Boolean> checkBoxesState;
    //...

    public mAdapter(Context context, ArrayList<String> posit, ArrayList<String> list, ArrayList<Boolean> checkBoxesState /* <- add this one*/) {
        //...
        this.checkBoxesState = checkBoxesState;
    }

如果他们得到检查或不做的话,请设置。

    @Override
    public void onBindViewHolder(final NotaViewHolder viewHolder, final int position) {

        //...
        viewHolder.checkBox.setChecked(checkBoxesState.get(position));
    }

现在你可能已经知道如何检查它们,但对于那些偶然发现它的人来说,这里是如何:

    //create adapter
    mAdapter = new mAdapter(context, posit, list, checkBoxesStates /* <- fill with false booleans */ );
    recyclerView.setAdapter(mAdapter);

    //check them all
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i <= mAdapter.getItemCount(); i++) {
                checkBoxesStates.add(i, true);
                mAdapter.notifyItemChanged(0); //this updates the adapter
            }
        }
    });

修改

您必须在ViewHolder中创建复选框,如下所示:

        public class NotaViewHolder extends RecyclerView.ViewHolder {

        //...
        CheckBox checkBox;


        public NotaViewHolder(final View itemView) {
            super(itemView);

            //...

            checkBox = (CheckBox) itemView.findViewById(R.id.rowCheckBox);

            //...
        }
    }

答案 1 :(得分:1)

执行此操作的最佳方式: 1.在适配器中添加一些代码:

private boolean isAllCheckBoxSelected;

public void setAllCheckBoxesSelected(boolean isSelected){
isAllCheckBoxSelected = isSelected;
notifyDataSetChanged();
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
...
holder.mCheckBox.setChecked(isAllCheckBoxSelected);
...
}

2。在您需要的片段调用中

mAdapter.setAllCheckBoxesSelected(true or false);