Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

android - Selecting one RadioButton value and scrolling back removing the selected one in RecyclerView

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;
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This problem occurs because of a bug in either RecyclerView or somewhere in the Android SDK. Anyway, I took the advice from another SO question - onCheckedChanged called automatically.

For RadioGroup, it will be a little different. There are two ways to go about. I recommend 1st one since it is plug-n-play.

  1. set OnCheckedChangeListener on RadioGroup:

    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId == -1) {
                Log.v("onCheck", "Android bug since RadioButton doesn't get unchecked normally!");
            }
            else {
                Log.v("onCheck", "Valid click. By user");
                mMyListObjectArr[position].setChecked(checkedId);
            }
        }
    });
    
  2. or set onCheckedChangeListener on all RadioButtons inside the RadioGroup:

    CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(buttonView.isPressed()) {
                Log.v("onCheck", position + ", valid click by user");
                mMyListObjectArr[position].setChecked(buttonView.getId());
            }
            else {
                Log.v("onCheck", "Android bug");
            }
        }
    };
    mRadioButton1.setOnCheckedChangeListener(onCheckedChangeListener);
    mRadioButton2.setOnCheckedChangeListener(onCheckedChangeListener);
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...