如何通过复选框获取Recyclerview中的选定项目计数,选择​​/取消选择所有项目?

时间:2019-01-11 06:46:20

标签: android android-recyclerview recycler-adapter android-checkbox

我有一个片段,其中包含回收视图中的项目列表。在它们下面是具有“不可见”状态的复选框。当单击一个项目时,我切换此状态。到目前为止,我已经做到了这一点,但不确定我是否使用了最佳方法。

这里的问题是,我希望能够从片段工具栏中的主复选框中选中/取消选中所有项目,还希望获得选中和未选中项目的总数,并将其发送到主机活动中。我总是得到错误的值,并且无法检查活动中的所有项目。这个实现对吗?

我已经正确初始化了所有视图并能够显示lsit,但是当我尝试将带有值的方法从适配器的viewholder传递到父片段时,没有任何反应。

片段代码://跳过了初始化

 public class MarkAttendanceFragment extends DialogFragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

private static final String TAG = MarkAttendanceFragment.class.getSimpleName();

private Toolbar toolbar;
private ImageView closeIV;
private TextView sessionNameAttendanceTV, totalTV, presentTV, absentTV;
private Button doneBtn;

private CheckBox checkAll;

private RecyclerView attRV;
private MarkAttendanceAdapter adapter;
private List<PlayerSession> playersList = new ArrayList<>();

private int numPresent = 0, numAbsent = 0, sumPlayers = 0;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_mark_attendance, container, false);

    init(view);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

    closeIV.setOnClickListener(this);
    checkAll.setOnCheckedChangeListener(this);
    doneBtn.setOnClickListener(this);

    return view;
}

private void init(View view) {
    numPresent = adapter.sendNumChecked();
    Log.d(TAG, "Num Checked:\t" + numPresent);
    presentTV.setText(numPresent + "");

    sumPlayers = adapter.getItemCount();
    Log.d(TAG, "Sum Players:\t" + sumPlayers);
    totalTV.setText(sumPlayers + "");

    numAbsent = sumPlayers - numPresent;
    Log.d(TAG, "Num Absent:\t" + numAbsent);
    absentTV.setText(numAbsent + "");
  }

并使用片段复选框的复合按钮检查所有方法:

 @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        adapter.checkAll();
    } else {
        adapter.unCheckAll();
    }
}

以下是适配器方法:

 @Override
public void onBindViewHolder(final MarkAttendanceViewHolder viewholder, int position) {
    final PlayerSession players = itemsList.get(position);
    viewholder.namePlayerTV.setText(players.getFirstName() + " " + players.getLastName());
    Picasso.with(context)
            .load(players.getImageURL())
            .placeholder(R.drawable.app_logo_resized)
            .into(viewholder.playerAvatarIV);

    final boolean isClicked = players.isSelected();
    viewholder.playerAvatarIV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    //                viewholder.checkBoxPlayer.setVisibility(isClicked ? View.VISIBLE : View.INVISIBLE);
   //                players.setSelected(!players.isSelected());
   //                players.setSelected(!isClicked);
   //                Log.d(TAG, "Sel Status " + players.isSelected());

   //                boolean isChecked = viewholder.checkBoxPlayer.isChecked();

            if (viewholder.checkBoxPlayer.isChecked()) {

    //                    if (selectedList.contains(players.getFirstName())) {
   //                        selectedList.remove(players.getFirstName());
   //                        Log.d(TAG, "Removed:\t" + players.getFirstName());
   //                    }

                viewholder.checkBoxPlayer.setChecked(false);
                viewholder.checkBoxPlayer.setVisibility(View.INVISIBLE);

            } else {
  //                    if (!selectedList.contains(players.getFirstName())) {
  //                        selectedList.add(players.getFirstName());
  //                        Log.d(TAG, "Added:\t" + players.getFirstName());
 //                    }
                viewholder.checkBoxPlayer.setChecked(true);
                viewholder.checkBoxPlayer.setVisibility(View.VISIBLE);
            }
        }
    });

    viewholder.checkBoxPlayer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d(TAG, "Checked:\t" + isChecked);
            if (isChecked) {
                viewholder.checkBoxPlayer.setVisibility(View.VISIBLE);
                players.setSelected(isChecked);
                numPresent++;
                Log.d(TAG, "Checkbox visible:\t and count:\t" + numPresent);
            } else {
                viewholder.checkBoxPlayer.setVisibility(View.INVISIBLE);
                players.setSelected(isChecked);
                numPresent--;
                Log.d(TAG, "Checkbox invisible:\t and count:\t" + numPresent);
            }
        }
    });
}

@Override
public int getItemCount() {
    if (itemsList == null) {
        return 0;
    }
    return itemsList.size();
}

public void checkAll() {
    vh.checkBoxPlayer.setChecked(true);
}

public void unCheckAll() {
    vh.checkBoxPlayer.setChecked(false);
}

public int sendNumChecked() {
    return numPresent;
}     

片段中的文本视图应该从回收的视图适配器以及所有项目处于选中状态或未选中状态时,获得检查状态的动态更新。如何完成此实施?谢谢。

1 个答案:

答案 0 :(得分:0)

我使用以下方法,它对我有用。您现在已定义onCheckedChanged(),当使用检查/取消选中片段中的复选框时,我使用迭代器将特定值分配给了数据源ArrayList并将适配器设置为list。

chk_select_all_users.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
   selectUsers(b);
}
});

现在在selectUsers()中,将boolean设置为true / false,然后再次设置适配器。

private void selectUsers(boolean check) {
    Iterator<PlayerSession> iterator = userdata.iterator();
    while (iterator.hasNext()) {
        PlayerSession temp = iterator.next();
        temp.setSelected(check);
    }

        //set adapter to list again.
}

您将需要根据模型中设置的值选中列表项复选框。因此,将以下代码放入onBindViewHolder()

if (userData.isSelected()) {
    viewholder.checkBoxPlayer.setChecked(true);
} else {
    viewholder.checkBoxPlayer.setChecked(false);
}