gpt4 book ai didi

android - 单击单个项目选择回收站 View 中的所有项目

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:55:44 25 4
gpt4 key购买 nike

我会稍微解释一下我的问题。在我的回收站 View 布局中有一个复选框,它在开始时是不可见的。当我点击回收站 View 中的一个项目时,我希望所有项目中的 checkbox 都可见。我在互联网上搜索了太久,找不到解决我的问题的方法。


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 文本。我可以通过单独单击项目来更改复选框的可见性。我只想让它在单击时更改所有项目。就像我们手机中的联系人列表一样。您只需长按项目,复选框就会出现在所有项目中。任何帮助,将不胜感激。最好的问候

最佳答案

我就是这样做的:

向您的 ViewAllInventoryDe​​vicesDetails 类添加一个新字段:

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()

关于android - 单击单个项目选择回收站 View 中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52222445/

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