java - 如何在 RecyclerView 中更新 CardViews 之间的点
问题描述
我正在开发一个测验应用程序,其中 RecyclerView 中的每个 CardView 有一个问题和五个单选按钮。每个单选按钮都有一个指定的分数。
比如有两个 CardView
卡片视图#1
- radio_button_1 = -20
- radio_button_2 = -10
- radio_button_3 = 0
- radio_button_4 = 10
- radio_button_5 = 20
卡片视图 #2
- radio_button_1 = -20
- radio_button_2 = -10
- radio_button_3 = 0
- radio_button_4 = 10
- radio_button_5 = 20
假设用户在 CardView #1 上选择 radio_button_1,在 CardView #2 上选择 radio_button_3,依此类推。我想在 CardViews 之间添加点,并能够在用户滚动 CardViews 时保留/保存这些点。我不知道它如何与 RecyclerView 一起工作。共享偏好?
我添加了一个文本视图来测试,看看分数是否在后台更新为新分数。
回收适配器:
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());
}
}}
主要活动:
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;
}
}
卡片视图 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>
解决方案
你可以声明一个interface
inside MainAdapter
,我们称之为ScoreUpdatesListener。它有一种方法,当您想要更新 textView 时将调用该方法。
您将需要在您的 中实现该接口MainActivity
,您可以匿名执行,或者让类本身实现该接口。喜欢class MainActivity implements MainAdapter.ScoreUpdatesListener {}
。
无论哪种方式,您都需要将该接口实现传递给适配器并保存其引用。您可以在构造函数中执行此操作,或者set
向适配器添加方法。
现在,当更新viewHolder
的值时,调用该侦听器,它将更新活动中的 textView。
class MainAdaptaer {
private ScoreUpdates listener;
...
class ViewHolder... {
...
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;
}
score += selectedValue;
// score is an existing attribute of MainAdapter
// it can and should be non static attribute
listener.updateScore(score);
}
}
interface ScoreUpdatesListener {
void onScoreUpdate(int score);
}
}
在您的活动中:
private void setupMainAdapter() {
List<App> apps = getApps();
MainAdapter adapter = new MainAdapter(apps, new MainAdapter.ScoreUpdatesListener() {
@Override
public void onScoreUpdate(int score) {
TextView valueView = titleTextView.findViewById(R.id.title);
valueView.setText(String.valueOf(score));
}
});
mRecyclerView.setAdapter(adapter);
}
如果类本身实现了接口,只需传递this
给适配器。
您不必将 TextView 处理移至活动,但您应该这样做。适配器不应该与不是ViewHolder
s 的视图混淆
推荐阅读
- pdf.js - 如何知道 pdfFindController.executeCommand 何时执行
- oracle - 如何查找在 2 个不同列中具有唯一值的最小记录
- php - Laravel 5.x 覆盖特定用户的视图
- swift - 传递给函数的 CoreData 类型
- ruby - Ruby/Kimurai/Capybara/Selenium/Chrome:仅当窗口可见时单击才有效。这是预期的行为吗?
- c# - 在 .Net Standard 中创建管道时出现 UnauthorizedAccessException
- c# - 将 JSON 日期时间转换为 C# 并忽略时区?
- cognos - 2019-01-01 到当前日期的 COGNOS 11 日期过滤器
- sha256 - 如何从内联 javascript 生成 SHA256 哈希?
- wordpress - 在古腾堡中隐藏简码预览