RecyclerView 中的深色模式颜色未正确解析

Dark mode color not resolved correctly in RecyclerView

我正在尝试实现夜间模式,但我遇到了问题:CardView 没有将颜色更改为 night 值,但 activity 正确地将其背景更改为夜间颜色值。

请帮助理解问题。

MainActivity

public class NewFinishActivity extends AppCompatActivity {

    @BindView(R.id.finishRecycler)
    RecyclerView finishRecycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

        setContentView(R.layout.activity_new_finish);
        ButterKnife.bind(this);


        LevelRecyclerAdapter mAdapter = new LevelRecyclerAdapter(getApplicationContext(), new ArrayList<>());
        finishRecycler.setAdapter(mAdapter);
    }
}

MainActivity布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color"
    tools:context=".activities.NewFinishActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/finishRecycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background_color"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</FrameLayout>

适配器

public class LevelRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<UserLevel> mItems;
    private Context mContext;

    public LevelRecyclerAdapter(Context context, List<UserLevel> items) {
        mContext = context;
        mItems = items;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AbstractCard card;
        card = new LevelCardView(mContext, parent);
        return card;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (!mItems.isEmpty()) {
            UserLevel data = mItems.get(position);
            LevelCardView card = (LevelCardView) viewHolder;
            card.bind(data);
        }
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    public void update(List<UserLevel> items) {
        mItems = items;
        notifyDataSetChanged();
    }
}

RecyclerView.ViewHolder

public class LevelCardView extends AbstractCard {

    Context mContext;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.levelCheck)
    AppCompatImageView levelCheck;
    @BindView(R.id.levelCard)
    CardView levelCard;

    public LevelCardView(Context context, ViewGroup parent) {
        this(context, LayoutInflater.from(context).inflate(R.layout.card_level, parent, false));
    }

    private LevelCardView(Context context, View view) {
        super(view, context);
        mContext = context;
        try {
            ButterKnife.bind(this, view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

RecyclerView.ViewHolder 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"
    android:id="@+id/levelCard"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    card_view:cardBackgroundColor="@color/background_color_card"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="@dimen/card_evalation">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:padding="15dp">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:drawableLeft="@drawable/ic_goal_1"
                android:fontFamily="@font/roboto"
                android:gravity="center"
                android:text="Lose Fat"
                android:textColor="@color/textColorStandard"
                android:textSize="20dp" />

        </RelativeLayout>

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/levelCheck"
            android:layout_width="70dp"
            android:visibility="gone"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/gender_selected" />
    </FrameLayout>

</androidx.cardview.widget.CardView>

colors.xml

<color name="textColorStandard">#282b35</color>
<color name="background_color">#fdfdfdfd</color>
<color name="background_color_card">#fff</color>
<color name="textColorStandardTransparent">#B222273D</color>

colors.xml 夜晚

<color name="textColorStandard">#FCFCFC</color>
<color name="background_color">#14182A</color>
<color name="background_color_card">#2F3550</color>
<color name="textColorAlwaysStandard">#282b35</color>

您使用的上下文不正确。您应该使用 activity.

的上下文,而不是使用应用程序的上下文

而不是执行:

card = new LevelCardView(mContext, parent);

执行以下操作:

card = new LevelCardView(parent.getContext(), parent);