Android:如何检查RecyclerView的项目中是否选中了复选框

时间:2018-08-10 03:39:24

标签: android android-recyclerview

我一直试图找出如何检查RecyclerView列表项中的复选框是否被选中的方法。我在网上看过一些文章,但我听不懂它们的含义。如果有人可以帮助,那就太好了。

有关我的应用的信息 这只是它的功能的小片段。这是一个联系人应用程序。在一个屏幕上,它列出了用户的所有联系人。我没有为此使用联系人选择器。我将列表显示为recyclerview,recyclerview中的每个项目都有一个复选框。用户可以选中要加注星标的复选框。当他们点击“保存”按钮时,他们选中的联系人将位于其“收藏夹”页面上,即MainActivity。

我现在需要找出如何查看回收站视图中是否已检查项目的内容。

这是适配器的代码:

package krispo.callaid;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by po on 9/8/2018.
 */

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

private List<Contact> contactsList;


public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView name, number;
    public CheckBox checkBox;
    public MyViewHolder(View view) {
        super(view);
        name = (TextView) view.findViewById(R.id.name);
        number = (TextView) view.findViewById(R.id.number);
        checkBox = (CheckBox)view.findViewById(R.id.checkBox);


    }
}


public ContactAdapter(List<Contact> contactsList) {
    this.contactsList = contactsList;
}

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

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Contact contact = contactsList.get(position);
    holder.name.setText(contact.getName());
    holder.number.setText(contact.getPhoneNumber());

}

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

}

这是联系人对象的代码:

package krispo.callaid;

import android.preference.CheckBoxPreference;

public class Contact {

private String name, phoneNumber,ID;
private boolean added;
private int position;


public Contact(){

}

    public Contact(String name, String phoneNumber,String ID, boolean added){
    this.name = name;
    this.phoneNumber = phoneNumber;
    this.ID = ID;
    this.added = added;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhoneNumber() {
    return this.phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public String getID() {
    return this.ID;
}

public void setID(String ID) {
    this.ID = ID;
}

public boolean isAdded() {
    return this.added;
}

public void setAdded(boolean added) {
    this.added = added;
}

public int getPosition() {
    return position;
}

public void setPosition(int position) {
    this.position = position;
}
}

这是每个项目行的代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/row_padding_vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/row_padding_vertical">

<TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/checkBox"
    android:layout_toRightOf="@+id/checkBox"
    android:text="Test"
    android:textColor="@android:color/black"
    android:textSize="25sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/name"
    android:layout_toEndOf="@+id/checkBox"
    android:layout_toRightOf="@+id/checkBox"
    android:text="Test"
    android:textColor="#7f8c8d"
    android:textSize="20sp" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toRightOf="@+id/number"
    android:layout_centerVertical="true" />

</RelativeLayout>

谢谢!我对此有点新意,如果有点杂乱,我感到抱歉。

3 个答案:

答案 0 :(得分:0)

更改适配器
-获取ID列表
-在检查时添加联系人ID

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

private List<Contact> contactsList;
private List<Integer> mSelectedItemsIds;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView name, number;
    public CheckBox checkBox;
    public MyViewHolder(View view) {
        super(view);
        name = (TextView) view.findViewById(R.id.name);
        number = (TextView) view.findViewById(R.id.number);
        checkBox = (CheckBox)view.findViewById(R.id.checkBox);


    }
}


public ContactAdapter(List<Contact> contactsList) {
    this.contactsList = contactsList;
    mSelectedItemsIds = new ArrayList<>();
}

public List<Integer> getSelectedIds() {
    return mSelectedItemsIds;
}

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

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Contact contact = contactsList.get(position);
    holder.name.setText(contact.getName());
    holder.number.setText(contact.getPhoneNumber());
    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                mSelectedItemsIds.add(position);
            }else{
                mSelectedItemsIds.remove(position);
            }
        }
    });

}

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

获取所选ID

  List<Integer> selectedid =  ContactAdapter.getSelectedIds();

放这个 用选定的ID做任何事情

答案 1 :(得分:0)

创建类型为{{1}的名为ArrayList的{​​{1}},并在checkedContacts方法中,为Contact设置检查监听器,并添加或删除联系人从onBindViewHolder中获取,如下所示:

CheckBox

答案 2 :(得分:0)

主要问题是,一旦您检查了一个项目并向上或向下滚动了回收站视图,使得该项目不在视图中,则回收站视图无法正确维护在重新创建视图时检查过的项目。为了保持选中项目的状态,我对适配器类进行了如下修改:

package krispo.callaid;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cashrich.cashrich.adapter.WithdrawCardAdapter;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by po on 9/8/2018.
 */

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

    private List<Contact> contactsList;
    private Boolean[] chkArr;


    public class MyViewHolder extends RecyclerView.ViewHolder implements CheckboxListener {
        public TextView name, number;
        public CheckBox checkBox;
        public CustomSwitchListener myCustomSwitchListener;
        public MyViewHolder(View view,CustomSwitchListener myCustomSwitchListener) {
            super(view);
            name = (TextView) view.findViewById(R.id.name);
            number = (TextView) view.findViewById(R.id.number);
            checkBox = (CheckBox)view.findViewById(R.id.checkBox);
            this.myCustomSwitchListener=myCustomSwitchListener;
            checkBox.setOnCheckedChangeListener(myCustomSwitchListener);
        }

        @Override
        public  void updateCheck(int pos,boolean val)
        {
            if(val)
            {
                checkBox.setChecked(true);
            }
            else
            {
                checkBox.setChecked(false);
            }
        }
    }

    public Boolean[] getSelectedIds() {
        return chkArr;
    }

    public ContactAdapter(List<Contact> contactsList) {
        this.contactsList = contactsList;
    }

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

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Contact contact = contactsList.get(position);
        holder.name.setText(contact.getName());
        holder.number.setText(contact.getPhoneNumber());

    }

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


    public interface CheckboxListener{
        void updateCheck(int pos,boolean val);
    }

    private class CustomSwitchListener implements CompoundButton.OnCheckedChangeListener {
        private int position;
        CheckboxListener checkboxListener;


        /**
         * Updates the position according to onBindViewHolder
         *
         * @param position - position of the focused item
         */
        public void updatePosition(int position,MyViewHolder holder) {
            this.position = position;
            checkboxListener=(CheckboxListener) holder;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            chkArr[position]=isChecked;
            if(isChecked)
            {
                checkboxListener.updateCheck(position,true);
            }
            else
            {
                checkboxListener.updateCheck(position,false);
            }
        }
    }
}

这实现了一个类,用于维护已检查项目的状态,并在重新创建视图时将其通知适配器。您可以使用此变量private Boolean[] chkArr;

获取所选项目的列表