我正在开发一款应用程序,要求客户选择不同问题的所有价格。我已经实现了recyclerview并在recyclerview上夸大了textview和checkbox,但问题是,当选中一个复选框时,它会自动选择另一个复选框,如果取消选中则自动取消选中第二个复选框。
这是我的代码
recyclerview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:background="@color/colorWhite"
tools:context="com.example.system2.tranxav.ProblemsActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:background="?attr/colorPrimary" />
<android.support.v7.widget.RecyclerView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingBottom="0dp"
android:orientation="vertical"
android:layout_marginBottom="0dp"
android:scrollbars="none"
android:overScrollMode="never"
android:clipToPadding="false"
android:id="@+id/problem_recycle_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
content.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="0dp"
android:paddingLeft="0dp"
android:paddingTop="0dp">
<android.support.v7.widget.CardView
android:elevation="3dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:id="@+id/card"
android:paddingTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvProblems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:textStyle="bold" />
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cbProblems"
android:layout_alignParentRight="true"
android:layout_weight="1" />
</LinearLayout>
</android.support.v7.widget.CardView>
myAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
Context context;
private List<Problems> mDataset;
private static final int TYPE_HEADER = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_ITEM = 2;
private ProblemListener setProblemListener;
CardView cv;
public void setProblemListener(ProblemListener problemListener){
this.setProblemListener = problemListener;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView tvProblems, tvPrice;
public Button btnProblem;
Button btnSubmitProblem;
CheckBox cbProblems;
public ViewHolder(View itemView) {
super(itemView);
tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
btnSubmitProblem = (Button) itemView.findViewById(R.id.btnProblem);
cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);
overrideFonts(context, tvProblems);
}
}
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView ) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/RobotoCondensedBold.ttf"));
}
} catch (Exception e) {
}
}
public void add(List<Problems> itemList){
mDataset = itemList;
notifyDataSetChanged();
}
public void remove(Problems item){
int position = mDataset.indexOf(item);
mDataset.remove(position);
notifyItemRemoved(position);
}
public MyAdapter(Context con, List<Problems> myDataset){
mDataset = myDataset;
this.context = con;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == TYPE_ITEM) {
//Inflating recycle view item layout
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_content, parent, false);
return new ItemViewHolder(itemView);
} else if (viewType == TYPE_HEADER) {
//Inflating header view
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_header, parent, false);
return new HeaderViewHolder(itemView);
} else if (viewType == TYPE_FOOTER) {
//Inflating footer view
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_footer, parent, false);
return new FooterViewHolder(itemView);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
try {
if (holder instanceof HeaderViewHolder) {
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.tvHeader.setText("Please kindly ask the mechanic what the problem is and check them to continue");
} else {
if (holder instanceof FooterViewHolder) {
final FooterViewHolder footerHolder = (FooterViewHolder) holder;
footerHolder.btnSubmitProblem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// footerHolder.cbProblems.setOnClickListener(null);
setProblemListener.onProblemSelected(v, mDataset, position);
}
});
} else if (holder instanceof ItemViewHolder) {
ItemViewHolder itemView = (ItemViewHolder) holder;
itemView.tvPrice.setText(mDataset.get(position - 1).getPrice());
itemView.tvProblems.setText(mDataset.get(position - 1).getProblems());
itemView.cbProblems.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mDataset.get(position - 1).setSelected(isChecked);
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return mDataset.size()+2;
}
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEADER;
} else if (position == mDataset.size() + 1) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
}
private class HeaderViewHolder extends MyAdapter.ViewHolder {
TextView tvHeader;
public HeaderViewHolder(View view) {
super(view);
tvHeader = (TextView) view.findViewById(R.id.tvHeader);
overrideFonts(context, tvHeader);
}
}
private class FooterViewHolder extends MyAdapter.ViewHolder {
Button btnSubmitProblem;
CheckBox cbProblems;
public FooterViewHolder(View view) {
super(view);
btnSubmitProblem = (Button) view.findViewById(R.id.btnProblem);
cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);
}
}
private class ItemViewHolder extends MyAdapter.ViewHolder {
TextView tvProblems, tvPrice;
public ItemViewHolder(View itemView) {
super(itemView);
tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
cv = (CardView)itemView.findViewById(R.id.card);
}
}
private class CheckboxViewHolder extends MyAdapter.ViewHolder {
public CheckboxViewHolder(View itemView) {
super(itemView);
cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);
}
}
public interface ProblemListener{
void onProblemSelected(View view, List<Problems> problem, int position);
}
}
oncreate方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_problems);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
mRecyclerView = (RecyclerView) findViewById(R.id.problem_recycle_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(ProblemsActivity.this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(ProblemsActivity.this, listProblems);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, PROBLEM_URL, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response)
{
mRecyclerView.setAdapter(mAdapter);
JSONParse jsonParse = new JSONParse(response);
jsonParse.parseJSON();
listProblems = jsonParse.getProblems();
mAdapter.add(listProblems);
mAdapter.notifyDataSetChanged();
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Info", "Failed");
error.printStackTrace();
}
});
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
requestQueue.add(stringRequest);
mRecyclerView.addItemDecoration(new DividerItemDecoration(ProblemsActivity.this, LinearLayoutManager.HORIZONTAL));
mAdapter.setProblemListener(new MyAdapter.ProblemListener() {
@Override
public void onProblemSelected(View view, List<Problems> problem, int position) {
double price = 0;
for (int i = 0; i < problem.size(); i++) {
Problems prob = problem.get(i);
if (prob.isSelected()) {
price += Double.parseDouble(prob.getPrice());
}
}
if (price == 0.0){
AlertDialog alertDialog = new AlertDialog.Builder(ProblemsActivity.this).create();
alertDialog.setTitle("Warning");
alertDialog.setMessage("Select problem solved to proceed");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}else{
AlertDialog alertDialog = new AlertDialog.Builder(ProblemsActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("The Total Amount is " + price);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Proceed >>>",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
}
});
}
答案 0 :(得分:0)
我通过添加以下行解决了这个问题
itemView.cbProblems.setChecked(mDataset.get(位置1).isSelected());
前
setOnCheckedChangeListener