作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码的结果是,只有在我向下滚动经过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/
我是一名优秀的程序员,十分优秀!