Итак, пошли, это полный код вашей цели
ObjectListItem.java
public class ObjectListItem {
private String itemTitle = "";
private boolean opt1, opt2, opt3;
private int selectedAnswerPosition = -1;
private boolean shine;
public String getItemTitle() {
return itemTitle;
}
public void setItemTitle(String itemTitle) {
this.itemTitle = itemTitle;
}
public boolean isOpt1() {
return opt1;
}
public void setOpt1(boolean opt1) {
this.opt1 = opt1;
if (opt1) {
setOpt2(false);
setOpt3(false);
}
}
public boolean isOpt2() {
return opt2;
}
public void setOpt2(boolean opt2) {
this.opt2 = opt2;
if (opt2) {
setOpt1(false);
setOpt3(false);
}
}
public boolean isOpt3() {
return opt3;
}
public void setOpt3(boolean opt3) {
this.opt3 = opt3;
if (opt3) {
setOpt1(false);
setOpt2(false);
}
}
public int getSelectedAnswerPosition() {
return selectedAnswerPosition;
}
public void setSelectedAnswerPosition(int selectedAnswerPosition) {
this.selectedAnswerPosition = selectedAnswerPosition;
}
public boolean isShine() {
return shine;
}
public void setShine(boolean shine) {
this.shine = shine;
}}
layout_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/root"
android:layout_height="wrap_content"
android:layout_marginEnd="7dp"
android:layout_marginStart="7dp"
android:layout_marginTop="7dp"
android:background="#989898"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="This is title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Option 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Option 2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Option 3" />
</LinearLayout>
</LinearLayout>
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.listItemsViewHolder> {
private Context context;
private ArrayList<ObjectListItem> objectListItems;
private OnRadioItemClicked onRadioItemClicked;
public void setOnRadioItemClicked(OnRadioItemClicked onRadioItemClicked) {
this.onRadioItemClicked = onRadioItemClicked;
}
public interface OnRadioItemClicked {
void radioItemChecked(int itemPosition, int optNum);
}
public MyAdapter(Context context, ArrayList<ObjectListItem> objectListItems) {
this.context = context;
this.objectListItems = objectListItems;
}
@Override
public listItemsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_list_item, parent, false);
return new MyAdapter.listItemsViewHolder(itemView);
}
@Override
public void onBindViewHolder(final listItemsViewHolder holder, int position) {
final ObjectListItem objectListItem = objectListItems.get(position);
holder.title.setText("This is my awesome title " + position);
holder.radioButton1.setChecked(objectListItem.isOpt1());
holder.radioButton2.setChecked(objectListItem.isOpt2());
holder.radioButton3.setChecked(objectListItem.isOpt3());
if (objectListItem.isShine()) {
holder.root.setBackgroundColor(ContextCompat.getColor(context, R.color.shine));
} else {
holder.root.setBackgroundColor(ContextCompat.getColor(context, R.color.normal));
}
}
@Override
public int getItemCount() {
return objectListItems.size();
}
public class listItemsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private LinearLayout root;
private TextView title;
private RadioButton radioButton1, radioButton2, radioButton3;
public listItemsViewHolder(View itemView) {
super(itemView);
root = itemView.findViewById(R.id.root);
title = itemView.findViewById(R.id.title);
radioButton1 = itemView.findViewById(R.id.radioButton1);
radioButton2 = itemView.findViewById(R.id.radioButton2);
radioButton3 = itemView.findViewById(R.id.radioButton3);
radioButton1.setOnClickListener(this);
radioButton2.setOnClickListener(this);
radioButton3.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.radioButton1:
if (onRadioItemClicked != null)
onRadioItemClicked.radioItemChecked(getAdapterPosition(), 1);
break;
case R.id.radioButton2:
if (onRadioItemClicked != null)
onRadioItemClicked.radioItemChecked(getAdapterPosition(), 2);
break;
case R.id.radioButton3:
if (onRadioItemClicked != null)
onRadioItemClicked.radioItemChecked(getAdapterPosition(), 3);
break;
}
}
}}
MainActivity.java
<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="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.AppCompatButton
android:id="@+id/button_check"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Check Answers" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements MyAdapter.OnRadioItemClicked, View.OnClickListener {
private MyAdapter myAdapter;
private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private ArrayList<ObjectListItem> objectListItems;
private Button buttonCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCheck = findViewById(R.id.button_check);
buttonCheck.setOnClickListener(this);
myRecyclerView = findViewById(R.id.recyclerView);
generateQuestions();
myAdapter = new MyAdapter(this, objectListItems);
myRecyclerView.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myAdapter.setOnRadioItemClicked(this);
myRecyclerView.setLayoutManager(linearLayoutManager);
myRecyclerView.setAdapter(myAdapter);
}
private void generateQuestions() {
objectListItems = new ArrayList<>();
for (int i = 0; i < 20; i++) {
ObjectListItem objectListItem = new ObjectListItem();
objectListItem.setItemTitle("This is my awesome title " + i);
objectListItems.add(objectListItem);
}
}
@Override
public void radioItemChecked(int itemPosition, int optNum) {
objectListItems.get(itemPosition).setSelectedAnswerPosition(optNum);
objectListItems.get(itemPosition).setShine(false);
switch (optNum) {
case 1:
objectListItems.get(itemPosition).setOpt1(true);
break;
case 2:
objectListItems.get(itemPosition).setOpt2(true);
break;
case 3:
objectListItems.get(itemPosition).setOpt3(true);
break;
}
myAdapter.notifyDataSetChanged();
}
@Override
public void onClick(View view) {
int result = checkAnswers();
if (result == -1) {
Toast.makeText(MainActivity.this, "We Have All Anwers ;)", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Answer to this question (ISHAK) :|", Toast.LENGTH_SHORT).show();
linearLayoutManager.smoothScrollToPosition(myRecyclerView, null, result);
objectListItems.get(result).setShine(true);
myAdapter.notifyDataSetChanged();
}
}
private int checkAnswers() {
for (int i = 0; i < objectListItems.size(); i++) {
if (objectListItems.get(i).getSelectedAnswerPosition() == -1) {
return i;
}
}
return -1;
}
}

Если пользователь нажимает кнопку «Проверить ответ», приложение автоматически переходит к первому неотвеченному вопросу и меняет его фон.
Полный код на my Github
счастливое кодирование;)