Я работаю над приложением для викторин, в котором есть один вопрос и пять переключателей для каждого CardView в RecyclerView. Каждый переключатель имеет назначенную оценку.
Например, есть два CardViews
CardView # 1
- radio_button_1 = -20
- radio_button_2 = -10
- radio_button_3 = 0
- radio_button_4 = 10
- radio_button_5 = 20
CardView # 2
- radio_button_1 = -20
- radio_button_2 = -10
- radio_button_3 = 0
- radio_button_4 = 10
- radio_button_5 = 20
Скажем, пользователь выбирает radio_button_1 в CardView # 1, а он выбирает radio_button_3 в CardView # 2 и так далее. Я хочу добавить точки между CardViews и иметь возможность сохранять / сохранять эти точки, когда пользователь прокручивает CardViews. Я не знаю, как это работает с RecyclerView. SharedPreferences?
Я добавил текстовое представление для проверки и проверки, обновляется ли оценка в фоновом режиме с помощью новой оценки.
Адаптер рециркуляции:
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
private List<App> mApps;
public static int score;
public static int updateScore() {
return score;
}
public MainAdapter(List<App> apps) {
mApps = apps;
}
@Override
public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_adapter, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public int getItemViewType(int position) {
return (position);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
App app = mApps.get(position);
holder.questionTextView.setText(app.getQuestion());
}
@Override
public int getItemCount() {
return mApps.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView questionTextView;
private TextView titleTextView;
public RadioGroup radioGroup;
public RadioButton rb1, rb2, rb3, rb4, rb5;
public ViewHolder(View itemView) {
super(itemView);
radioGroup = itemView.findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int selectedValue = 0;
switch (checkedId) {
case R.id.radio_button_1:
selectedValue -= 20;
break;
case R.id.radio_button_2:
selectedValue -= 10;
break;
case R.id.radio_button_3:
selectedValue += 0;
break;
case R.id.radio_button_4:
selectedValue += 10;
break;
case R.id.radio_button_5:
selectedValue += 20;
break;
}
updateValue (selectedValue);
}
public int updateValue(int selectedValue) {
/**Only to test if value is being tallied**/
TextView valueView = titleTextView.findViewById(R.id.title);
valueView.setText(String.valueOf(selectedValue));
return selectedValue;
}
});
questionTextView = itemView.findViewById(R.id.question);
titleTextView = itemView.findViewById(R.id.title);
rb1 = itemView.findViewById(R.id.radio_button_1);
rb2 = itemView.findViewById(R.id.radio_button_2);
rb3 = itemView.findViewById(R.id.radio_button_3);
rb4 = itemView.findViewById(R.id.radio_button_4);
rb5 = itemView.findViewById(R.id.radio_button_5);
}
@Override
public void onClick(View v) {
Log.d("App", mApps.get(getAdapterPosition()).getQuestion());
}
}}
MainActivity:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
setupMainAdapter();
}
private void setupMainAdapter() {
List<App> apps = getApps();
MainAdapter adapter = new MainAdapter(apps);
mRecyclerView.setAdapter(adapter);
}
private List<App> getApps() {
List<App> apps = new ArrayList<>();
apps.add(new App((getResources().getString(R.string.question_1))));
apps.add(new App((getResources().getString(R.string.question_2))));
apps.add(new App((getResources().getString(R.string.question_3))));
apps.add(new App((getResources().getString(R.string.question_4))));
apps.add(new App((getResources().getString(R.string.question_5))));
apps.add(new App((getResources().getString(R.string.question_6))));
apps.add(new App((getResources().getString(R.string.question_7))));
apps.add(new App((getResources().getString(R.string.question_8))));
apps.add(new App((getResources().getString(R.string.question_9))));
apps.add(new App((getResources().getString(R.string.question_10))));
return apps;
}
}
CardView XML
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="275dp"
android:layout_height="575dp"
android:layout_margin="16dp"
card_view:cardBackgroundColor="@color/colorAccent"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="@font/futura_medium"
android:padding="24dp"
android:textColor="#000000"
android:textSize="32sp"
tools:text="@string/title" />
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_centerHorizontal="true"
android:fontFamily="@font/futura_medium"
android:textColor="#000000"
android:textSize="18sp"
tools:text="@string/question" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimaryDark"
android:text="-2"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radio_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimaryDark"
android:text="-1"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radio_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimaryDark"
android:text="0"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radio_button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimaryDark"
android:text="1"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radio_button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimaryDark"
android:text="2"
android:textColor="#000000" />
</RadioGroup>
<com.google.android.material.button.MaterialButton
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
android:layout_marginBottom="32dp"
android:fontFamily="@font/futura_medium"
android:text="@string/submit"
android:textColor="@color/colorAccent"
android:textSize="16sp"
app:backgroundTint="@color/colorPrimaryDark"
app:rippleColor="@color/colorPrimary" />
</RelativeLayout>
</androidx.cardview.widget.CardView>