如何更新RecyclerView中的CardViews之间的点

问题描述 投票:0回答:1

我正在开发一个测验应用程序,在RecyclerView中每个CardView有一个问题和五个单选按钮。每个单选按钮都有一个指定的分数。

例如,有两个CardView

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

假设用户在CardView#1上选择radio_button_1,他在CardView#2上选择radio_button_3,依此类推。我想在CardViews之间添加点,并且当用户滚动浏览CardView时能够保存/保存这些点。我不知道它如何与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());
        }
    }}

主要内容:

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>
java android radio-button cardview
1个回答
0
投票

你可以在interface中声明一个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处理移动到活动,但您应该这样做。适配器不应该混淆不是ViewHolders的视图

© www.soinside.com 2019 - 2024. All rights reserved.