通过单击单个项目选择Recycler视图中的所有项目
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过单击单个项目选择Recycler视图中的所有项目相关的知识,希望对你有一定的参考价值。
我会稍微解释一下我的问题。我的回收站视图布局中有一个复选框,一开始就不可见。当我点击回收器视图中的一个项目时,我希望在所有项目中都可以看到该复选框。我在互联网上搜索了太久,找不到解决问题的方法。
public class ViewInventoryListAdapter extends RecyclerView.Adapter<ViewInventoryListAdapter.ListViewHolder> {
private Context context;
List<ViewAllInventoryDevicesDetails> list;
private Button assignButton;
private UserLoginResponseModel userLoginResponseModel;
private android.support.v4.app.Fragment fragment;
private FragmentManager fragmentManager;
private FragmentActivity myContext;
private Activity activity;
private int counter = 0;
private List<String> selectedDeviceSerialNumbers;
private static CheckBox checkBox;
public ViewInventoryListAdapter(Context context, List<ViewAllInventoryDevicesDetails> list, Button assign, UserLoginResponseModel model, Activity activity ) {
this.context = context;
this.list = list;
this.assignButton = assign;
this.userLoginResponseModel = model;
this.activity =activity;
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.view_all_inventory_listlayout_cardview,null);
//Specifying Activity for Fragment Transaction
activity = activity;
myContext = (FragmentActivity) activity;
//Iniallizing Fragment Manager
fragmentManager = myContext.getSupportFragmentManager();
selectedDeviceSerialNumbers = new ArrayList<>();
checkBox = view.findViewById(R.id.view_inventory_checkbox);
return new ViewInventoryListAdapter.ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ListViewHolder holder, final int position) {
Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);
holder.deviceSerialNumber.setText(list.get(position).getSerialNumber());
holder.deviceType.setText(list.get(position).getDeviceType());
FontManager.markAsIconContainer(holder.deviceIcon, iconFont);
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
holder.isSelected.setVisibility(View.VISIBLE);
//holder.isSelected.setVisibility(View.VISIBLE);
holder.isSelected.setChecked(true);
assignButton.setVisibility(View.VISIBLE);
return false;
}
});
holder.isSelected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.isSelected.setChecked(false);
holder.isSelected.setVisibility(View.INVISIBLE);
//Login For Visibility For assign Button
counter--;
selectedDeviceSerialNumbers.remove(list.get(position).getSerialNumber());
if(counter == 0){
assignButton.setVisibility(View.GONE);
}
}
});
assignButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
if(userLoginResponseModel != null){
bundle.putSerializable("UserModel", userLoginResponseModel);
bundle.putSerializable("serialNumbers", (Serializable) selectedDeviceSerialNumbers);
}
fragment = new AssingSelectedDevicesFragment();
fragment.setArguments(bundle);
fragmentManager.beginTransaction()
.replace(R.id.flContent, fragment)
.commit();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class ListViewHolder extends RecyclerView.ViewHolder{
private TextView deviceType, deviceSerialNumber, deviceIcon;
private CheckBox isSelected;
public ListViewHolder(View itemView){
super(itemView);
deviceType = itemView.findViewById(R.id.view_inventory_deviceType_textview);
deviceSerialNumber = itemView.findViewById(R.id.view_inventory_serialNumber_textview);
deviceIcon = itemView.findViewById(R.id.view_inventory_image_textview);
isSelected = itemView.findViewById(R.id.view_inventory_checkbox);
}
}
}
这是我的OnBindViewHolder文本。我可以通过单独单击项目来更改复选框可见性。我只想让它在单击时更改所有项目。就像我们手机中的联系人列表一样。你只需按下项目,复选框就会显示在所有项目中。任何帮助,将不胜感激。最好的祝福
这就是我这样做的方式:
在ViewAllInventoryDevicesDetails
类中添加一个新字段:
public boolean showCheckbox = false;
向适配器添加一些辅助方法:
private void showAllBoxes() {
for (ViewAllInventoryDevicesDetails item : list) {
item.showCheckbox = true;
}
notifyDataSetChanged();
}
private void hideAllBoxes() {
for (ViewAllInventoryDevicesDetails item : list) {
item.showCheckbox = false;
}
notifyDataSetChanged();
}
然后,在您的onBindViewHolder()
方法中,使用该标志:
@Override
public void onBindViewHolder(@NonNull final ListViewHolder holder, final int position) {
ViewAllInventoryDevicesDetails item = list.get(position);
holder.isSelected.setVisibility(item.showCheckbox ? View.VISIBLE : View.GONE);
}
当你想要显示所有内容时,你只需要调用showAllBoxes()
,然后再次使用hideAllBoxes()
来隐藏它们。
你需要在ViewAllInventoryDevicesDetails class
中添加一个布尔变量来检查是否选择了项目并选择了checkbox
或者不选择boolean isCheckSelected = false; //默认值为false,因为在启动时未选择任何值。
在这里我修改了你的代码:
public class ViewInventoryListAdapter extends RecyclerView.Adapter<ViewInventoryListAdapter.ListViewHolder> {
private Context context;
List<ViewAllInventoryDevicesDetails> list;
private Button assignButton;
private UserLoginResponseModel userLoginResponseModel;
private android.support.v4.app.Fragment fragment;
private FragmentManager fragmentManager;
private FragmentActivity myContext;
private Activity activity;
private int counter = 0;
private List<String> selectedDeviceSerialNumbers;
private static CheckBox checkBox;
private int selectedItem = -1; // -1 due to first time nothing selected
public ViewInventoryListAdapter(Context context, List<ViewAllInventoryDevicesDetails> list, Button assign, UserLoginResponseModel model, Activity activity) {
this.context = context;
this.list = list;
this.assignButton = assign;
this.userLoginResponseModel = model;
this.activity = activity;
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.view_all_inventory_listlayout_cardview, null);
//Specifying Activity for Fragment Transaction
activity = activity;
myContext = (FragmentActivity) activity;
//Iniallizing Fragment Manager
fragmentManager = myContext.getSupportFragmentManager();
selectedDeviceSerialNumbers = new ArrayList<>();
checkBox = view.findViewById(R.id.view_inventory_checkbox);
return new ViewInventoryListAdapter.ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ListViewHolder holder, final int position) {
Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);
holder.deviceSerialNumber.setText(list.get(position).getSerialNumber());
holder.deviceType.setText(list.get(position).getDeviceType());
FontManager.markAsIconContainer(holder.deviceIcon, iconFont);
if (selectedItem != -1 && selectedItem == position) {
holder.isSelected.setVisibility(View.VISIBLE);
//holder.isSelected.setVisibility(View.VISIBLE);
assignButton.setVisibility(View.VISIBLE);
} else {
holder.isSelected.setVisibility(View.GONE);
assignButton.setVisibility(View.GONE);
}
if (selectedDeviceSerialNumbers.get(position).getCheckSelected()) {
holder.isSelected.setChecked(true);
} else {
holder.isSelected.setChecked(true);
}
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (selectedItem != -1) {
selectedItem = position;
showAllBoxes();
} else {
selectedItem = -1;
hideAllBoxes();
}
return false;
}
});
holder.isSelected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedDeviceSerialNumbers.get(position).setCheckSelected(!selectedDeviceSerialNumbers.get(position).getCheckSelected());
notifyDataSetChanged();
}
});
assignButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
if (userLoginResponseModel != null) {
bundle.putSerializable("UserModel", userLoginResponseModel);
bundle.putSerializable("serialNumbers", (Serializable) selectedDeviceSerialNumbers);
}
fragment = new AssingSelectedDevicesFragment();
fragment.setArguments(bundle);
fragmentManager.beginTransaction()
.replace(R.id.flContent, fragment)
.commit();
}
});
}
private void showAllBoxes() {
for (ViewAllInventoryDevicesDetails item : list) {
item.showCheckbox = true;
}
notifyDataSetChanged();
}
private void hideAllBoxes() {
for (ViewAllInventoryDevicesDetails item : list) {
item.showCheckbox = false;
}
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return list.size();
}
class ListViewHolder extends RecyclerView.ViewHolder {
private TextView deviceType, deviceSerialNumber, deviceIcon;
private CheckBox isSelected;
public ListViewHolder(View itemView) {
super(itemView);
deviceType = itemView.findViewById(R.id.view_inventory_deviceType_textview);
deviceSerialNumber = itemView.findViewById(R.id.view_inventory_serialNumber_textview);
deviceIcon = itemView.findViewById(R.id.view_inventory_image_textview);
isSelected = itemView.findViewById(R.id.view_inventory_checkbox);
}
}
}
我希望它会对你有所帮助。
你好你也可以通过回调来实现这一点(接口)
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ListViewHolder> {
ArrayList<ListItemModel> myArrayList;
ListClicklistener listClicklistener;
public MyListAdapter(ArrayList<ListItemModel> myArrayList, ListClicklistener listClicklistener) {
this.myArrayList = myArrayList;
this.listClicklistener = listClicklistener;
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new ListViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull ListViewHolder listViewHolder, int i) {
listViewHolder.tvTitle.setText(myArrayList.get(i).title);
if (myArrayList.get(i).isCheckBoxShow) {
listViewHolder.chkBox.setVisibility(View.VISIBLE);
}
listViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listClicklistener.onListItemCli以上是关于通过单击单个项目选择Recycler视图中的所有项目的主要内容,如果未能解决你的问题,请参考以下文章
如何从android studio中的recycler视图将数据添加到SQLite数据库中