我正在尝试使用RecyclerView
个元素制作一个CardView
,每个元素都有不同的颜色。我正在使用适配器并将颜色的资源传递给我。
问题是我的元素没有使用我传递的颜色(例如红色,蓝色或绿色),而是使用了不同的紫色阴影。
如果我对子级相对布局使用相同的代码,则颜色可以很好地工作,但它们不会填满整个卡片。
这是列表的元素:
<android.support.v7.widget.CardView
android:id="@+id/rl_main_list_background_color"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="16dp"
android:layout_marginBottom="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_main_list_game_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game"
android:layout_margin="16dp"
android:textStyle="bold|italic"
android:textSize="20sp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
这是适配器
public class GamesListAdapter extends RecyclerView.Adapter<GamesListAdapter.GamesViewHolder> {
private ArrayList<GameItem> mGamesList;
public static class GamesViewHolder extends RecyclerView.ViewHolder{
public TextView mGameName;
public CardView mItemRelativeLayour;
public GamesViewHolder(@NonNull View itemView) {
super(itemView);
mGameName = itemView.findViewById(R.id.tv_main_list_game_name);
mItemRelativeLayour = itemView.findViewById(R.id.rl_main_list_background_color);
}
}
public GamesListAdapter(ArrayList<GameItem> gamesList){ mGamesList = gamesList;}
@NonNull
@Override
public GamesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_list_list_element, viewGroup, false);
GamesViewHolder gamesViewHolder = new GamesViewHolder(view);
return gamesViewHolder;
}
@Override
public void onBindViewHolder(@NonNull GamesViewHolder gamesViewHolder, int i) {
GameItem currentItem = mGamesList.get(i);
gamesViewHolder.mGameName.setText(currentItem.getGameName());
gamesViewHolder.mItemRelativeLayour.setCardBackgroundColor(currentItem.getBackgroundColor());
}
@Override
public int getItemCount() {
return mGamesList.size();
}
}
这就是我填充列表的方式
gamesList = new ArrayList<>();
gamesList.add(new GameItem("Truth or Dare", R.color.truthOrDareColor));
gamesList.add(new GameItem("Kings", R.color.kingsColor));
gamesList.add(new GameItem("Flip the card(Urmatoarea Carte)", R.color.flipTheCardColor));
gamesList.add(new GameItem("Most likely to", R.color.mostLikelyToColor));
gamesList.add(new GameItem("Spin the bottle", R.color.spinTheBottleColor));
gamesList.add(new GameItem("I have sex like", R.color.iHaveSexLikeColor));
gamesList.add(new GameItem("Never have I ever", R.color.neverHaveIEverColor));
关于如何解决此问题的任何想法?谢谢!
答案 0 :(得分:0)
更改
gamesViewHolder.mItemRelativeLayour.setCardBackgroundColor(currentItem.getBackgroundColor());
收件人
gamesViewHolder.mItemRelativeLayour.setCardBackgroundColor(
ContextCompat.getColor(context, currentItem.getBackgroundColor()));
您将Interger
ID设置为颜色R.color.some_color
只是存储了对颜色的引用,您必须从该ID获取实际颜色。
要从参考ID中获取颜色,您应该使用以下内容,
ContextCompat.getColor(context, R.color.some_color);
快乐编码。