gpt4 book ai didi

java - 如何让按钮选择 recyclerView 中的所有复选框?

转载 作者:行者123 更新时间:2023-12-02 11:43:08 25 4
gpt4 key购买 nike

我的代码的结果是,只有我向下滚动经过recyclerView中的复选框后才选中所有复选框 - 当我向上滚动时,它们会被选中.

如何在单击按钮后立即检查它们?

在我的 Activity 中,我有:

    private void publicButton() {

publicContacts.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//loop through the contacts
int count = PopulistoContactsAdapter.theContactsList.size();

for (int i = 0; i < count; i++) {
PopulistoContactsAdapter.theContactsList.get(i).setSelected(true);
}
}
});
}

对于recyclerViewAdapter:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
//bind the views into the ViewHolder
//selectPhoneContact is an instance of the SelectPhoneContact class.
//We will assign each row of the recyclerview to contain details of selectPhoneContact:

//The number of rows will match the number of phone contacts
final SelectPhoneContact selectPhoneContact = theContactsList.get(position);

//if the row is a matching contact
if (viewHolder.getItemViewType() == 1)

{
//in the title textbox in the row, put the corresponding name etc...
((MatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
((MatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
//((MatchingContact) viewHolder).check.setText("Cheeckbox" + position);
((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).isSelected);
((MatchingContact) viewHolder).check.setTag(position);

((MatchingContact) viewHolder).check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//pos is the row number that the clicked checkbox exists in
Integer pos = (Integer) ((MatchingContact) viewHolder).check.getTag();

Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " clicked!", Toast.LENGTH_SHORT).show();

if (theContactsList.get(pos).getSelected()) {
theContactsList.get(pos).setSelected(false);
} else {
theContactsList.get(pos).setSelected(true);
}
}
});

}
}

Adapter 中的 onClick 按单个复选框的计划工作;他们的状态被保留。只是当我尝试使用 Activity 中的按钮选择所有这些时,问题就出现了。

最佳答案

到目前为止,最简单的答案是在进行所有 setSelected() 调用后,在适配器上简单地调用 notifyDataSetChanged()

从您的代码中,我无法判断 PopulistoContactsAdapter 是否是适配器实例,或者 PopulistoContactsAdapter.theContactsList 是否是静态变量...您需要一个适配器实例调用 notifyDataSetChanged()。如果您只有对 RecyclerView 的引用,则可以调用 myRecyclerView.getAdapter().notifyDataSetChanged()

关于java - 如何让按钮选择 recyclerView 中的所有复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48390885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com