选择一个 RadioButton 值并回滚删除 RecyclerView 中的选定值 [英] Selecting one RadioButton value and scrolling back removing the selected one in RecyclerView

查看:24
本文介绍了选择一个 RadioButton 值并回滚删除 RecyclerView 中的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在 RecyclerView 的帮助下显示了 20 道选择题.

如果我更改第一个 RadioGroup 的值并向下滚动,再次向上滚动删除 RecycelarView 中的选定值,并且我想使用选定的 RadioButton 值进一步,我还检查了

In my application am displaying 20 multiple choice questions with the help of RecyclerView.

If I change the value of first RadioGroup and scrolls down, again scrolls up removing the selected value in RecycelarView and also i want to use that selected RadioButton value further, I was also checked link1 but i did't understood what he is doing.

Here is my sample code snippet let me know if you need any clarification.

    package com.testing.survey;

import java.util.List;

import android.annotation.SuppressLint;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

@SuppressWarnings("rawtypes")
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {

    private List<Student> stList;

    public DataAdapter(List<Student> students) {
        this.stList = students;
    }

    // Create new views
    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
            int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.list_row, null);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);

        return viewHolder;
    }

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

        final int pos = position;

        viewHolder.tvQuestionNumber.setText(stList.get(position).getQuestionNumber());

        viewHolder.tvQuestion.setText(stList.get(position).getQuestion());

        viewHolder.rbAns1.setText(stList.get(position).getAnswer1());

        viewHolder.rbAns2.setText(stList.get(position).getAnswer2());

        viewHolder.rbAns3.setText(stList.get(position).getAnswer3());

        viewHolder.rbAns4.setText(stList.get(position).getAnswer4());

        viewHolder.rbAns5.setText(stList.get(position).getAnswer4());

        //viewHolder.rgAnswers.clearCheck();

        viewHolder.rgAnswers.check(stList.get(position).getSelectedRadioButtonId());
        viewHolder.rgAnswers.setTag(new Integer(position));


        Log.v("select"+position,stList.get(position).getSelectedRadioButtonId()+"");

        viewHolder.rgAnswers.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                int radioButtonID = group.getCheckedRadioButtonId();
                View radioButton = group.findViewById(radioButtonID);
                int clickedPos = ((Integer)group.getTag()).intValue(); 

                //Student contact=(Student)group.getTag();

                //contact.setSelectedRadioButtonId(radioButtonID);
                stList.get(clickedPos).setSelectedRadioButtonId(radioButtonID);


                Log.v("hello"+clickedPos,stList.get(clickedPos).getSelectedRadioButtonId()+"");
            }
        });

    }

    // Return the size arraylist
    @Override
    public int getItemCount() {
        return stList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvQuestionNumber;
        public TextView tvQuestion;

        public RadioGroup rgAnswers;
        public RadioButton rbAns1,rbAns2,rbAns3,rbAns4,rbAns5;

        public Student singlestudent;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            tvQuestionNumber = (TextView) itemLayoutView.findViewById(R.id.tvQuestionNumber);

            tvQuestion = (TextView) itemLayoutView.findViewById(R.id.tvQuestion);
            rgAnswers=(RadioGroup)itemLayoutView.findViewById(R.id.rgAnswers);
            rbAns1=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer1);
            rbAns2=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer2);
            rbAns3=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer3);
            rbAns4=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer4);
            rbAns5=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer5);

        }

    }

    // method to access in activity after updating selection
    public List<Student> getStudentist() {
        return stList;
    }

}

解决方案

I created a sample code that will work as per your req.

First we have to create the xml as follows :

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerViewAppList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>

Then we will create an interface as follows

public interface OnOptionSelected {
public void onOptionSelected(int position,int itemSelected);
}

Then we will create the model class as follows :

public class QuestionModel {

private String question;
private int seleectedAnswerPosition;
private boolean op1Sel,op2Sel,op3Sel; // options 

public boolean isOp1Sel() {
    return op1Sel;
   }

public void setOp1Sel(boolean op1Sel) {
    this.op1Sel = op1Sel;
    if(op1Sel){ // To make sure only one option is selected at a time
        setOp2Sel(false);
        setOp3Sel(false);
    }
   }

    public boolean isOp2Sel() {
      return op2Sel;
   }

   public void setOp2Sel(boolean op2Sel) {
    this.op2Sel = op2Sel;
    if(op2Sel){
        setOp1Sel(false);
        setOp3Sel(false);
     }
      }

 public boolean isOp3Sel() {
    return op3Sel;
     }

 public void setOp3Sel(boolean op3Sel) {
    this.op3Sel = op3Sel;
    if(op3Sel){
        setOp2Sel(false);
        setOp1Sel(false);
     }
    }

    public int getSeleectedAnswerPosition() {
    return seleectedAnswerPosition;
    }

  public void setSeleectedAnswerPosition(int seleectedAnswerPosition) {
    this.seleectedAnswerPosition = seleectedAnswerPosition;
   }

  public String getQuestion() {
    return question;
  }
 public void setQuestion(String question) {
    this.question = question;
  }
     } 

Then we will create the view for row :

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

    <RadioButton
        android:id="@+id/radoptionOne"
        android:text="May Be"
        android:checked="false"
        android:saveEnabled="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RadioButton
        android:checked="false"
        android:id="@+id/radoptionTwo"
        android:text="NO"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RadioButton
        android:checked="false"
        android:id="@+id/radoptionThree"
        android:text="Yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
   </LinearLayout>

Adapter class as follows:

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

   private List<QuestionModel> questionModels;


public void setOnOptionSelected(OnOptionSelected onOptionSelected) {
    this.onOptionSelected = onOptionSelected;
}

private OnOptionSelected onOptionSelected;


public List<QuestionModel> getQuestionModels() {
    return questionModels;
}

public void setQuestionModels(List<QuestionModel> questionModels) {
    this.questionModels = questionModels;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView question;
    RadioGroup radioGroup;
    RadioButton op1, op2, op3;

    ViewHolder(View view) {
        super(view);
        question = (TextView) view.findViewById(R.id.question);
        //radioGroup=(RadioGroup)view.findViewById(R.id.radGroup);
        op1 = (RadioButton) view.findViewById(R.id.radoptionOne);
        op2 = (RadioButton) view.findViewById(R.id.radoptionTwo);
        op3 = (RadioButton) view.findViewById(R.id.radoptionThree);
        op1.setOnClickListener(this);
        op2.setOnClickListener(this);
        op3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.radoptionOne:
                onOptionSelected.onOptionSelected(getAdapterPosition(), 1);
                break;

            case R.id.radoptionTwo:
                onOptionSelected.onOptionSelected(getAdapterPosition(), 2);
                break;

            case R.id.radoptionThree:
                onOptionSelected.onOptionSelected(getAdapterPosition(), 3);
                break;
        }
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v;
    // create a normal view
    v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_view, parent, false);
    return new ViewHolder(v);

}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    viewHolder.question.setText(questionModels.get(position).getQuestion());

    Log.e("POSITION" + position, "1" + questionModels.get(position).isOp1Sel());

    Log.e("POSITION" + position, "2" + questionModels.get(position).isOp2Sel());
    Log.e("POSITION" + position, "3" + questionModels.get(position).isOp3Sel());


    viewHolder.op1.setChecked(questionModels.get(position).isOp1Sel());
    viewHolder.op2.setChecked(questionModels.get(position).isOp2Sel());
    viewHolder.op3.setChecked(questionModels.get(position).isOp3Sel());


}

@Override
public int getItemCount() {
    if (questionModels != null) {
        return questionModels.size();
    }
    return 0;
}
}

Then the Activity class :

public class MainActivity extends Activity implements OnOptionSelected{

private RecyclerView mRecyclerView;
private List<QuestionModel> questionModels;
private  QuestionAdapter questionAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRecyclerView =(RecyclerView)findViewById(R.id.recyclerViewAppList);
    mRecyclerView.setHasFixedSize(true);
    questionModels=new ArrayList<QuestionModel>();
    for (int i=0;i<20;i++)
    {
        QuestionModel questionModel=new QuestionModel();
        questionModel.setQuestion("Question " + (i + 1));
        questionModels.add(questionModel);
    }
    questionAdapter =new QuestionAdapter();
    questionAdapter.setOnOptionSelected(this);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    questionAdapter.setQuestionModels(questionModels);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setAdapter(questionAdapter);
}


@Override
public void onOptionSelected(int position, int itemSelected) {
    questionModels.get(position).setSeleectedAnswerPosition(itemSelected);
    switch (itemSelected){
        case 1:
            questionModels.get(position).setOp1Sel(true);
            break;

        case 2:
            questionModels.get(position).setOp2Sel(true);
            break;
        case 3:
            ((QuestionModel)questionModels.get(position)).setOp3Sel(true);
            break;
    }
    questionAdapter.setQuestionModels(questionModels);
    questionAdapter.notifyDataSetChanged();
   // mRecyclerView.setAdapter(questionAdapter);

}
 }

I tried the solution with Radiogroup but it was not working properly( sometime it was retaining the old value). Then I changed the way of the selecting options in my model class.

这篇关于选择一个 RadioButton 值并回滚删除 RecyclerView 中的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆