单击单个项目,在“回​​收者视图”中选择所有项目

时间:2018-09-07 12:19:25

标签: android android-recyclerview recycler-adapter recyclerview-layout

我会解释一下我的问题。我的“回收者”视图布局中有一个复选框,一开始它是不可见的。当我单击“回收站”视图中的一项时,我希望在所有项目中可见复选框。我在互联网上搜索了太长时间,找不到解决我问题的方法。


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

3 个答案:

答案 0 :(得分:2)

这就是我要做的事情:

向您的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()

答案 1 :(得分:1)

您需要在ViewAllInventoryDevicesDetails class中添加一个布尔变量,以检查是否选择了项目以及是否选择了checkbox 布尔isCheckSelected = 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);
        }

    }
}

希望对您有帮助。

答案 2 :(得分:0)

您好,您也可以通过使用回调(接口)来实现此目的

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.onListItemClick();
            }
        });
    }

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

    public class ListViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        CheckBox chkBox;

        public ListViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            chkBox = itemView.findViewById(R.id.chkBox);
        }
    }
}

采用一个界面

public interface ListClicklistener {
    void onListItemClick();
}

带回调的SetAdapter

myListAdapter = new MyListAdapter(myArrayList, new ListClicklistener() {
            @Override
            public void onListItemClick() {
                for (int i = 0; i < myArrayList.size(); i++) {
                    myArrayList.get(i).isCheckBoxShow = true;
                }
                myListAdapter.notifyDataSetChanged();
            }
        });