gpt4 book ai didi

java - 如何从 Activity 的弹出菜单中选择回收者 View 的复选框?

转载 作者:行者123 更新时间:2023-12-01 21:17:10 25 4
gpt4 key购买 nike

我有一个带有复选框的联系人列表。另外,我还必须在工具栏上单击图像时创建一个弹出菜单。

弹出菜单显示全选选项。

所以单击全选我希望列表中的所有复选框都被选中。

我在适配器中定义的复选框。

适配器:

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

private ArrayList<Contact> contactArrayList;
private Context mContext;

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
private CheckBox checkBox;

public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.textContactName);
checkBox = (CheckBox) view.findViewById(R.id.checkBox);

}
}


public InviteAdapter(Context context, ArrayList<Contact> contactArrayList) {
this.contactArrayList = contactArrayList;
this.mContext = context;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.invite_contact_item, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Contact contact = contactArrayList.get(position);
holder.name.setText(contact.getmFullName());



}

@Override
public int getItemCount() {
return contactArrayList.size();
}

}

Activity :

    public class InviteContactsActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private InviteAdapter adapter;
private ArrayList<Contact> contactList;
private ContactTableHelper contactDb;


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


Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
TextView mTitle = (TextView) findViewById(R.id.toolbar_title);

mTitle.setText("Invite Contacts");


if (toolbar != null) {

toolbar.setTitle("");
setSupportActionBar(toolbar);

}
ImageView back = (ImageView)findViewById(R.id.imageBack);

back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

finish();

}
});

final ImageView selectAll = (ImageView)findViewById(R.id.selectAll);

selectAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

PopupMenu popup = new PopupMenu(InviteContactsActivity.this,selectAll);

popup.getMenuInflater().inflate(R.menu.pop_up_menu, popup.getMenu());

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {



return true;
}
});

popup.show();
}
});


contactDb = new ContactTableHelper(InviteContactsActivity.this);

contactList = contactDb.getAllContacts();

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(InviteContactsActivity.this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());

adapter = new InviteAdapter(InviteContactsActivity.this, contactList);
recyclerView.setAdapter(adapter);
}
}

谢谢你..

编辑:

我是这样做的:

Activity 中:

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {

for (Contact contact: mContactList) {

contact.setSelected(true);


}

mAdapter.notifyItemRangeChanged(); // how to do here?

return true;
}
});

在适配器中:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Contact contact = contactArrayList.get(position);
holder.name.setText(contact.getmFullName());

if(contact.getSelected() == true)
{
holder.checkBox.setChecked(true);
}

}

请检查并告诉我我缺少什么?

最佳答案

在适配器类中创建一个方法:

public void toggleContactsSelection( boolean isSelected ) {
for( Contact contact : contactArrayList ) {
contact.setSelected(isSelected);
}
notifyDataSetChanged(); // OR you can use notifyItemRangeChanged - which ever suits your needs
}

在你的 onBindViewHolder 方法中:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Contact contact = contactArrayList.get(position);
holder.name.setText(contact.getmFullName());
holder.checkBox.setChecked(contact.getSelected());
}

每当您需要选择所有/取消选择所有联系人时,调用适配器的方法:

inviteAdapter.toggleContactsSelection(true); //Selects all contacts
inviteAdapter.toggleContactsSelection(false); //Unselect all contacts

以下是 Recycler 适配器中涉及的方法及其使用说明的链接:

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html

关于java - 如何从 Activity 的弹出菜单中选择回收者 View 的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39828209/

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