显示recyclelerview复选框,根据布尔值android选择和取消选中

时间:2017-11-16 03:27:44

标签: android android-recyclerview android-checkbox

让我告诉你我到底需要什么。我需要从MySql DB加载20个问题到recyclerview。问题保存在一个包含两列的表中,即id和question。我可以在适配器的帮助下成功将这些问题加载到我的recyclerview中。这是扭曲,我需要在我的recyclerview中将这些问题标记为“是”或“否”,根据用户在他的个人资料中提出的问题之前确切记录的内容,使用复选框。此外,我期望良好的数据库和对象类设计解决方案,以实现这一目标。

这是我尝试过的,

适配器类:

public class MyHealthInfoAdapter extends RecyclerView.Adapter<MyHealthInfoAdapter.MyHealthViewHolder> {

private Context context;
private ArrayList<DonorHealthStatusQuestionare> listQuestions;

public MyHealthInfoAdapter(Context context, ArrayList<DonorHealthStatusQuestionare> listQuestions) {
    this.context = context;
    this.listQuestions = listQuestions;
}

@Override
public MyHealthViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_my_health_info, parent, false);
    return new MyHealthViewHolder(view);
}

@Override
public void onBindViewHolder(final MyHealthViewHolder holder, int position) {

    DonorHealthStatusQuestionare donorHealthStatusQuestionare = listQuestions.get(position);
    holder.tvQuestion.setText(donorHealthStatusQuestionare.getQuestions());

}

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

public class MyHealthViewHolder extends RecyclerView.ViewHolder{

    CardView cvParent;
    TextView tvQuestion;
    CheckBox cbYes, cbNo;

    public MyHealthViewHolder(View itemView) {
        super(itemView);
        cvParent = itemView.findViewById(R.id.card_view_my_health_status_rv);
        tvQuestion = itemView.findViewById(R.id.tv_eligibility_question_my_info);
        cbYes = itemView.findViewById(R.id.checkBoxYes_my_health_info);
        cbNo = itemView.findViewById(R.id.checkBoxNo_my_health_info);

    }
}    
}

片段:

public class DonorHealthStatus extends Fragment {

private static final int MY_SOCKET_TIMEOUT_MS = 100000;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private DonorHealthStatusAdapter donorHealthStatusAdapter;
private ArrayList<DonorHealthStatusQuestionare> donorHealthStatusQuestionares = new ArrayList<>();
private HealthStatusCheck statusCheck = new HealthStatusCheck();
private String donorID;
private Donor donor;
private boolean checked;


public DonorHealthStatus() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_donor_health_status, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    donor = ((DisplayDonorDetails)getActivity()).getDonor();
    donorID = donor.getUserID();

    getHealthStatus(new HealthStatusCallBack() {
        @Override
        public void onSuccess(HealthStatusCheck healthStatusCheck) {
            statusCheck = healthStatusCheck;
            setHealthStatusView();
        }

        @Override
        public void onFail(String msg) {}
    });

}

// SETTING HEALTH STATUS POJO CLASS FROM DATABASE.
private void getHealthStatus(final HealthStatusCallBack healthStatusCallBack) {
    Utility.checkNetworkConnectionBoolean(getActivity());

    if(Utility.isNetworkEnabled){
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GET_HEALTH_STATUS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {

                            JSONArray jsonArray = new JSONArray(response);
                            JSONObject jsonObject = jsonArray.getJSONObject(0);
                            String code = jsonObject.getString("code");

                            if(code.equals("0001")){

                                statusCheck.setId(Integer.valueOf(jsonObject.getString("id")));
                                statusCheck.setDonorID(Integer.valueOf(jsonObject.getString("donorID")));

                                if(jsonObject.getString("q1").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q1").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q2").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q2").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q3").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q3").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q4").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q4").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q5").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q5").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q6").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q6").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q7").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q7").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q8").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q8").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q9").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q9").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q10").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q10").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q11").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q11").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q12").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q12").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q13").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q13").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q14").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q14").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q15").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q15").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q16").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q16").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q17").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q17").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q18").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q18").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q19").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q19").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q20").equals("0")){
                                    checked = false;
                                    statusCheck.setQ1(checked);
                                }else if(jsonObject.getString("q20").equals("1")){
                                    checked = true;
                                    statusCheck.setQ1(checked);
                                }

                            }

                            healthStatusCallBack.onSuccess(statusCheck);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {}
                }
        ){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("donorID", donorID);
                return params;
            }
        };

        stringRequest.setRetryPolicy(new DefaultRetryPolicy(MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        NetworkRequestSingleTon.getOurInstance(getActivity()).addToRequestQue(stringRequest);
    }else {
        Utility.checkNetworkConnectionAlert(getActivity());
    }



}

// SETTING RECYCLER VIEW OF QUESTIONARE FROM DATABASE.
private void setHealthStatusView() {

    recyclerView = getActivity().findViewById(R.id.rv_donor_health_info);
    layoutManager = new LinearLayoutManager(getActivity());

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    donorHealthStatusAdapter = new DonorHealthStatusAdapter(getActivity(),
            donorHealthStatusQuestionares, statusCheck);

    recyclerView.setAdapter(donorHealthStatusAdapter);

    Utility.checkGpsConnectionBoolean(getActivity());
    if(Utility.isNetworkEnabled){

        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GET_HEALTH_QUESTIONARE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {

                            JSONArray jsonArray = new JSONArray(response);

                            int count = 0;

                            while (count < jsonArray.length()){
                                JSONObject jsonObject = jsonArray.getJSONObject(count);

                                DonorHealthStatusQuestionare donorHealthStatusQuestionare =
                                        new DonorHealthStatusQuestionare();

                                donorHealthStatusQuestionare.setId(Integer.valueOf(jsonObject.getString("id")));
                                donorHealthStatusQuestionare.setQuestions(jsonObject.getString("questions"));

                                donorHealthStatusQuestionares.add(donorHealthStatusQuestionare);
                                count ++;

                            }

                            donorHealthStatusAdapter.notifyDataSetChanged();


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        NetworkRequestSingleTon.getOurInstance(getActivity()).addToRequestQue(stringRequest);

    }else {
        Utility.checkNetworkConnectionAlert(getActivity());
    }

}

// CALL BACK INTERFACE TO GET POJO CLASS FROM VOLLEY BACKGROUND ACTIVITY.
public interface HealthStatusCallBack{

    void onSuccess(HealthStatusCheck healthStatusCheck);

    void onFail(String msg);
}
}

布局XML:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view_health_status_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:padding="12dp">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/tv_eligibility_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>


    <RelativeLayout
        android:id="@+id/health_status_radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0">

        <CheckBox
            android:id="@+id/checkBoxYes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="@string/yes" />

        <CheckBox
            android:layout_toRightOf="@+id/checkBoxYes"
            android:layout_toEndOf="@+id/checkBoxYes"
            android:id="@+id/checkBoxNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no" />

    </RelativeLayout>


</LinearLayout>

结果检查POJO类:

public class HealthStatusCheck {
private int id;
private int donorID;
private boolean q1;
private boolean q2;
private boolean q3;
private boolean q4;
private boolean q5;
private boolean q6;
private boolean q7;
private boolean q8;
private boolean q9;
private boolean q10;
private boolean q11;
private boolean q12;
private boolean q13;
private boolean q14;
private boolean q15;
private boolean q16;
private boolean q17;
private boolean q18;
private boolean q19;
private boolean q20;

public HealthStatusCheck() {}

public HealthStatusCheck(int id, int donorID, boolean q1, boolean q2, boolean q3, boolean q4, boolean q5, boolean q6,
                         boolean q7, boolean q8, boolean q9, boolean q10, boolean q11, boolean q12, boolean q13,
                         boolean q14, boolean q15, boolean q16, boolean q17, boolean q18, boolean q19, boolean q20) {
    this.id = id;
    this.donorID = donorID;
    this.q1 = q1;
    this.q2 = q2;
    this.q3 = q3;
    this.q4 = q4;
    this.q5 = q5;
    this.q6 = q6;
    this.q7 = q7;
    this.q8 = q8;
    this.q9 = q9;
    this.q10 = q10;
    this.q11 = q11;
    this.q12 = q12;
    this.q13 = q13;
    this.q14 = q14;
    this.q15 = q15;
    this.q16 = q16;
    this.q17 = q17;
    this.q18 = q18;
    this.q19 = q19;
    this.q20 = q20;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getDonorID() {
    return donorID;
}

public void setDonorID(int donorID) {
    this.donorID = donorID;
}

public boolean isQ1() {
    return q1;
}

public void setQ1(boolean q1) {
    this.q1 = q1;
}

public boolean isQ2() {
    return q2;
}

public void setQ2(boolean q2) {
    this.q2 = q2;
}

public boolean isQ3() {
    return q3;
}

public void setQ3(boolean q3) {
    this.q3 = q3;
}

public boolean isQ4() {
    return q4;
}

public void setQ4(boolean q4) {
    this.q4 = q4;
}

public boolean isQ5() {
    return q5;
}

public void setQ5(boolean q5) {
    this.q5 = q5;
}

public boolean isQ6() {
    return q6;
}

public void setQ6(boolean q6) {
    this.q6 = q6;
}

public boolean isQ7() {
    return q7;
}

public void setQ7(boolean q7) {
    this.q7 = q7;
}

public boolean isQ8() {
    return q8;
}

public void setQ8(boolean q8) {
    this.q8 = q8;
}

public boolean isQ9() {
    return q9;
}

public void setQ9(boolean q9) {
    this.q9 = q9;
}

public boolean isQ10() {
    return q10;
}

public void setQ10(boolean q10) {
    this.q10 = q10;
}

public boolean isQ11() {
    return q11;
}

public void setQ11(boolean q11) {
    this.q11 = q11;
}

public boolean isQ12() {
    return q12;
}

public void setQ12(boolean q12) {
    this.q12 = q12;
}

public boolean isQ13() {
    return q13;
}

public void setQ13(boolean q13) {
    this.q13 = q13;
}

public boolean isQ14() {
    return q14;
}

public void setQ14(boolean q14) {
    this.q14 = q14;
}

public boolean isQ15() {
    return q15;
}

public void setQ15(boolean q15) {
    this.q15 = q15;
}

public boolean isQ16() {
    return q16;
}

public void setQ16(boolean q16) {
    this.q16 = q16;
}

public boolean isQ17() {
    return q17;
}

public void setQ17(boolean q17) {
    this.q17 = q17;
}

public boolean isQ18() {
    return q18;
}

public void setQ18(boolean q18) {
    this.q18 = q18;
}

public boolean isQ19() {
    return q19;
}

public void setQ19(boolean q19) {
    this.q19 = q19;
}

public boolean isQ20() {
    return q20;
}

public void setQ20(boolean q20) {
    this.q20 = q20;
}
}

问题POJO课程:

public class DonorHealthStatusQuestionare {
private int id;
private String questions;
boolean isSelected;

public DonorHealthStatusQuestionare() {}

public DonorHealthStatusQuestionare(int id, String questions) {
    this.id = id;
    this.questions = questions;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getQuestions() {
    return questions;
}

public void setQuestions(String questions) {
    this.questions = questions;
}

public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean selected) {
    isSelected = selected;
}
}

3 个答案:

答案 0 :(得分:0)

您可以使用pojo中的布尔变量来设置复选框的状态。在onBindViewHoldercheckbox.setchecked(yourBooleanVariable);中添加此代码,并在xml(android:enabled="false")或代码(checkbox.setEnabled(false);)中将复选框设置为已停用

答案 1 :(得分:0)

为了显示带有POJO类复选框的recyleview,这里是完整的解决方案。

POJO课程:

import java.io.Serializable;

public class Student implements Serializable {


private static final long serialVersionUID = 1L;
private String name;
private String emailId;
private boolean isSelected;

public Student() {

}

public Student(String name, String emailId) {

 this.name = name;
 this.emailId = emailId;

}

public Student(String name, String emailId, boolean isSelected) {

this.name = name;
this.emailId = emailId;
this.isSelected = isSelected;
}

public String getName() {
return name;
}

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

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}

public boolean isSelected() {
return isSelected;
}

public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
 }

} 

<强>活动:

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CardViewActivity extends AppCompatActivity {

 private Toolbar toolbar;
 private RecyclerView mRecyclerView;
 private RecyclerView.Adapter mAdapter;
 private RecyclerView.LayoutManager mLayoutManager;
 private List<Student> studentList;
 private Button btnSelection;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  toolbar = (Toolbar) findViewById(R.id.toolbar);
  btnSelection = (Button) findViewById(R.id.btnShow);  
  studentList = new ArrayList<Student>();

  for (int i = 1; i <= 15; i++) {
   Student st = new Student("Student " + i, "androidstudent" + i
     + "@gmail.com", false);

   studentList.add(st);
  }

  if (toolbar != null) {
   setSupportActionBar(toolbar);
   getSupportActionBar().setTitle("Android Students");

  }
  mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
  mRecyclerView.setHasFixedSize(true);
  mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  mAdapter = new CardViewDataAdapter(studentList);
  mRecyclerView.setAdapter(mAdapter);

  btnSelection.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    String data = "";
    List<Student> stList = ((CardViewDataAdapter) mAdapter)
      .getStudentist();

    for (int i = 0; i < stList.size(); i++) {
     Student singleStudent = stList.get(i);
     if (singleStudent.isSelected() == true) {
      data = data + "\n" + singleStudent.getName().toString();
     }
    }
   }});
 }
}

活动XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:scrollbars="vertical" />

    <Button
        android:id="@+id/btnShow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="Show Selected"
        android:background="#00796B" 
        android:textColor="@color/TextPrimaryColor"/>

</LinearLayout>

卡片适配器:

import java.util.List;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CardViewDataAdapter extends
  RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
 private List<Student> stList;
 public CardViewDataAdapter(List<Student> students) {
  this.stList = students;

 }


 @Override
 public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
   int viewType) {
  View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
    R.layout.cardview_row, null);
  ViewHolder viewHolder = new ViewHolder(itemLayoutView);
  return viewHolder;
 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int position) {

  final int pos = position;
  viewHolder.tvName.setText(stList.get(position).getName());
  viewHolder.tvEmailId.setText(stList.get(position).getEmailId());
  viewHolder.chkSelected.setChecked(stList.get(position).isSelected());
  viewHolder.chkSelected.setTag(stList.get(position));


  viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    CheckBox cb = (CheckBox) v;
    Student contact = (Student) cb.getTag();
    contact.setSelected(cb.isChecked());
    stList.get(pos).setSelected(cb.isChecked());
   }
  });
 }
 // Return the size arraylist
 @Override
 public int getItemCount() {
  return stList.size();
 }

 public static class ViewHolder extends RecyclerView.ViewHolder {

  public TextView tvName;
  public TextView tvEmailId;
  public CheckBox chkSelected;
  public Student singlestudent;

  public ViewHolder(View itemLayoutView) {
   super(itemLayoutView);
   tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);
   tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId);
   chkSelected = (CheckBox) itemLayoutView.findViewById(R.id.chkSelected);
  }
 }
 public List<Student> getStudentist() {
  return stList;
 }

}

卡片行

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    card_view:cardCornerRadius="5dp"

    card_view:cardUseCompatPadding="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="name"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvEmailId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tvName"
            android:text="email"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/chkSelected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            />
    </RelativeLayout>

</android.support.v7.widget.CardView>

快乐的编码!!

答案 2 :(得分:0)

Checkbox在Adapter XMl文件中启用为false,如下面的代码

 <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"/>

然后在活动

中设置recycleleriew适配器,如下所示
TestAdapter testAdpter = new TestAdapter(this, testList);
recyclerview.setAdapter(testAdpter);

然后将适配器设置如下

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

private final Activity mContext;
private final List<TestData> mList;
private final LayoutInflater inflater;

public TestAdapter(Activity activity, List<TestData> testList) {
    this.mContext = activity;
    this.mList = testList;
    this.inflater = LayoutInflater.from(mContext);
}

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

  @Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    if (mList.get(position).isSelected()) {
        holder.cb.setChecked(true);
    } else {
        holder.cb.setChecked(false);
    }

  }

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

public class ViewHolder extends RecyclerView.ViewHolder {
    private final CheckBox cb;

    public ViewHolder(View view) {
        super(view);
         cb = (CheckBox) view.findViewById(R.id.cb);
    }
}
}