简体   繁体   English

Android-如何更改RecyclerView的小部件的可见性并将其显示在recyclerView的所有项目中?

[英]Android - How to change the visibility of a widget of and show it in all items in recyclerView?

I have a RecyclerView in my activity and I want to add Multiple selection to the RecyclerView , and this is my Adapter: 我的活动中有一个RecyclerView ,我想向RecyclerView添加多个选择,这是我的适配器:

public class RingtoneItemAdabter extends RecyclerView.Adapter<RingtoneItemAdabter.ViewHolder> {

    private Context context;
    private Activity activity;
    private List<RingtoneItem> list;
    private boolean selectionMode = false;
    private List<RingtoneItem> selectedItems = new ArrayList<>();

    public class ViewHolder extends RecyclerView.ViewHolder {

        public Switch aSwitch;
        public TextView textView;
        public View view;
        public CheckBox checkBox;

        public ViewHolder(View view) {
            super(view);
            this.view = view;
            aSwitch = (Switch) view.findViewById(R.id.ringtone_state_switch);
            textView = (TextView) view.findViewById(R.id.ringtone_title_textview);
            checkBox = (CheckBox) view.findViewById(R.id.selection_checkbox);
        }
    }

    public RingtoneItemAdabter(Context context, Activity activity, List<RingtoneItem> list) {
        this.context = context;
        this.activity = activity;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.recyclerview_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final RingtoneItem item = list.get(position);

        holder.checkBox.setVisibility(selectionMode ? View.VISIBLE : View.GONE);
        holder.textView.setText(item.getTitle());
        holder.aSwitch.setChecked(item.isActive());
        holder.textView.setTextColor(item.isActive() ? Color.BLACK : Color.GRAY);
        holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    holder.textView.setTextColor(Color.BLACK);
                    item.setActive(true);
                    writeToFile();
                } else {
                    holder.textView.setTextColor(Color.GRAY);
                    item.setActive(false);
                    writeToFile();
                }
            }
        });
        holder.view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(selectionMode){
                    return false;
                }else {
                    startSelecting();
                    holder.checkBox.setChecked(true);
                    return true;
                }
            }
        });
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(selectionMode){
                    holder.checkBox.setChecked(!holder.checkBox.isChecked());
                }
            }
        });
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    selectedItems.add(item);
                }else {
                    selectedItems.remove(item);
                }
                if(selectedItems.size() == 0){
                    stopSelecting();
                }
            }
        });
    }

    @Override
    public int getItemCount() { ... }

    private void writeToFile() { ... }

    private void startSelecting(){
        selectionMode = true;
        notifyDataSetChanged();
    }

    private void stopSelecting(){
        selectionMode = false;
        notifyDataSetChanged();
    }
}

and this is the checkbox tag in recyclerview_item : 这是recyclerview_item中的复选框标签:

    <CheckBox
        android:id="@+id/selection_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

so I want the startSelection method to show the checkbox in every item, and the stopSelection method to hide the checkbox in every item. 所以我想要startSelection方法在每个项目中显示复选框,而stopSelection方法在每个项目中隐藏复选框。

EDIT: 编辑:

It's looks like I got a problem, the problem is now when I click and hold on an item the checkboxes appear and the checkbox of the item I long clicked must get checked, and if I unchecked every box, the checkboxed should disappear,this is how the app should work, but the when I tried it, it's didn't work, when I long click an item the checkboxes appear in some cases another checkbox get checked, but in another cases no one of the checkboxes get checked, and when I uncheck every checkbox, the checkboxes stay there. 看来我遇到了问题,问题是当我单击并按住某个项目时,将出现复选框,并且必须选中我长时间单击的项目的复选框,如果我未选中每个复选框,则复选框应消失,这是应用程序的工作方式,但是当我尝试使用它时,它不起作用,当我长时间单击某个项目时,该复选框会在某些情况下出现,另一个复选框会被选中,但在另一些情况下,其中一个复选框都不会被选中,何时我取消选中每个复选框,这些复选框保留在那里。 I have an idea about why the checkboxed don't disappear, because for example if I long clicked the fourth item, the checkboxes will appear, but rather than the fourth item which should get checked the first item will get checked, but the fourth item is the one which will added to selectedItems list, so if I unchecked the first box nothing will happen because it's not stored in the list, but if I checked the fourth item, then another fourth item will added to the list, and if I unchecked it, one of the two copies of the fourth item will removed, but one will stay. 我有一个关于为什么复选框不消失的想法,例如,如果我长按了第四项,则将显示复选框,而不是应该选中的第四项,而不是第一项被选中的东西是将添加到selectedItems列表中的那个,因此,如果我取消选中第一个框,则不会发生任何事情,因为它没有存储在列表中,但是如果我选中了第四项,则另一个第四项将添加到列表中,并且如果我未选中它将删除第四项的两个副本之一,但将保留其中一个。 check the code above. 检查上面的代码。

I know my explain is very bad, and my English isn't very good, but hope you understand :) 我知道我的解释很不好,我的英语也不太好,但是希望您能理解:)

Define a boolean variable for show/hide checkbox state and in onBindViewHolder set visibility of your checkbox related to that variable 为显示/隐藏复选框状态定义一个布尔变量,并在onBindViewHolder设置与该变量相关的复选框的可见性

boolean showCheckboxes = false;

public void startSelection() {
    showCheckboxes = true;
    notifyDataSetChanged();
}

public void stopSelection() {
    showCheckboxes = false;
    notifyDataSetChanged();
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    final RingtoneItem item = list.get(position);

    holder.checkBox.setVisibility(showCheckboxes ? View.VISIBLE: View.GONE);

    /*
    ...
    */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM