我已经通过 RecyclerView 制作了 CardView 的适配器供我使用相同的卡片模板来实现我的这个功能。
目标是根据inc_status
中的参数INCCards.java
创建具有不同颜色的某些卡。但它似乎并没有起作用。
这是模板卡的源代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/spacing_medium"
android:paddingRight="@dimen/spacing_medium"
android:paddingTop="@dimen/spacing_medium"
android:background="@color/tertiary">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="@dimen/spacing_none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/spacing_large"
android:paddingRight="@dimen/spacing_large"
android:paddingTop="@dimen/spacing_large"
android:paddingBottom="@dimen/spacing_medium"
android:id="@+id/relative_layout">
<TextView
android:id="@+id/course_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textColor="@color/white"
android:textSize="@dimen/text_headline"
android:text="@string/course_code"
android:textStyle="bold"/>
<TextView
android:id="@+id/course_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/course_code"
android:textColor="@color/white"
android:textSize="@dimen/text_subhead"
android:text="@string/course_title" />
<TextView
android:id="@+id/faculty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:layout_below="@+id/course_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textSize="@dimen/text_body"
android:text="@string/faculty" />
<ImageView
android:id="@+id/status_icon"
android:src="@drawable/icon_avatar"
android:layout_width="@dimen/size_user_icon"
android:layout_height="@dimen/size_user_icon"
android:layout_above="@+id/faculty"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/inc_grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/text_body"
android:layout_below="@+id/status_icon"
android:layout_alignRight="@+id/status_icon"
android:layout_alignEnd="@+id/status_icon"
android:layout_alignLeft="@+id/status_icon"
android:layout_alignStart="@+id/status_icon"
android:gravity="center"
android:textAlignment="center"
android:textColor="@color/white"
android:text="@string/equiv_grade"/>
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="0.001dp"
android:background="#FFFFFF"
android:id="@+id/line_divider"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/spacing_large"
android:paddingRight="@dimen/spacing_medium"
android:paddingBottom="@dimen/spacing_medium"
android:layout_marginTop="@dimen/spacing_medium"
android:id="@+id/semesterInfoLinearLayout">
<TextView
android:id="@+id/section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/text_caption"
android:text="@string/section"
android:textColor="@color/white"
android:layout_weight="0.33" />
<TextView
android:id="@+id/semester"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/text_caption"
android:text="@string/semester"
android:textColor="@color/white"
android:layout_weight="0.33"
android:gravity="center" />
<TextView
android:id="@+id/acad_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/text_caption"
android:text="@string/acad_year"
android:textColor="@color/white"
android:layout_weight=".33"
android:gravity="right" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
对于片段布局:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_inc_cards"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tertiary"/>
到对象初始化每张卡的位置
package ph.edu.amitypipduo.myiit;
public class INCCards {
String course_code, course_title, faculty, section, semester, acad_year;
int inc_status, status_icon;
final String inc_grade = "INC 3.00";
public INCCards(String course_code, String course_title, String faculty, String section, String semester, String acad_year, String inc_status) {
this.course_code = course_code;
this.course_title = course_title;
this.faculty = faculty;
this.section = section;
this.semester = semester;
this. acad_year = acad_year;
switch (inc_status) {
case "notice":
this.inc_status = R.color.inc_notice;
this.status_icon = R.drawable.inc_notice;
break;
case "alert":
this.inc_status = R.color.inc_alert;
this.status_icon = R.drawable.inc_alert;
break;
case "warning":
this.inc_status = R.color.inc_warning;
this.status_icon = R.drawable.inc_warning;
break;
case "danger":
this.inc_status = R.color.inc_danger;
this.status_icon = R.drawable.inc_danger;
break;
}
}
}
我尝试按方法onBindViewHolder
设置卡片的背景颜色:
cardViewHolder.card_view.setCardBackgroundColor(inc_cards.get(i).inc_status);
如
中所示package ph.edu.amitypipduo.myiit;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class INCAdapter extends RecyclerView.Adapter<INCAdapter.CardViewHolder> {
List<INCCards> inc_cards;
public INCAdapter(List<INCCards> inc_cards){
this.inc_cards = inc_cards;
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_inc_card, viewGroup, false);
CardViewHolder card_view_holder = new CardViewHolder(view);
return card_view_holder;
}
@Override
public void onBindViewHolder(CardViewHolder cardViewHolder, int i) {
cardViewHolder.course_code.setText(inc_cards.get(i).course_code);
cardViewHolder.course_title.setText(inc_cards.get(i).course_title);
cardViewHolder.faculty.setText(inc_cards.get(i).faculty);
cardViewHolder.section.setText(inc_cards.get(i).section);
cardViewHolder.semester.setText(inc_cards.get(i).semester);
cardViewHolder.acad_year.setText(inc_cards.get(i).acad_year);
cardViewHolder.inc_grade.setText(inc_cards.get(i).inc_grade);
cardViewHolder.status_icon.setImageResource(inc_cards.get(i).status_icon);
cardViewHolder.card_view.setCardBackgroundColor(inc_cards.get(i).inc_status);
}
@Override
public int getItemCount() {
return inc_cards.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
CardView card_view;
TextView course_code, course_title, faculty, section, semester, acad_year, inc_grade;
ImageView status_icon;
CardViewHolder(View itemView) {
super(itemView);
card_view = (CardView) itemView.findViewById(R.id.card_view);
course_code = (TextView) itemView.findViewById(R.id.course_code);
course_title = (TextView) itemView.findViewById(R.id.course_title);
faculty = (TextView) itemView.findViewById(R.id.faculty);
inc_grade = (TextView) itemView.findViewById(R.id.inc_grade);
section = (TextView) itemView.findViewById(R.id.section);
semester = (TextView) itemView.findViewById(R.id.semester);
acad_year = (TextView) itemView.findViewById(R.id.acad_year);
status_icon = (ImageView) itemView.findViewById(R.id.status_icon);
}
}
}
然后初始化片段类中的数据并使布局膨胀:
package ph.edu.amitypipduo.myiit;
import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class INCMonitorFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private RecyclerView recycler_view;
private LinearLayoutManager linear_layout_manager;
private INCAdapter inc_adapter;
private List<INCCards> inc_cards;
private void initializeData() {
inc_cards = new ArrayList<>();
inc_cards.add(new INCCards("CSC 198", "Methods of Research", "Prof. Cyrus Gabilla", "CS-1A", "SECOND SEMESTER", "AY 2014-2015", "danger"));
inc_cards.add(new INCCards("POLSCI 2", "Philippine Govt. & Const.", "Prof. Cyrus Gabilla", "AB4", "SUMMER SEMESTER", "AY 2013-2014", "warning"));
inc_cards.add(new INCCards("ENG 2N", "Writing in Discipline", "Prof. Rabindranath Polito", "B4", "FIRST SEMESTER", "AY 2012-2013", "alert"));
inc_cards.add(new INCCards("MATH 51", "I Forgot the Course Title", "Prof. Forgotten Name", "69", "SECOND SEMESTER", "AY 2012-2013", "notice"));
}
// TODO: Rename and change types and number of parameters
public static INCMonitorFragment newInstance(String param1, String param2) {
INCMonitorFragment fragment = new INCMonitorFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public INCMonitorFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
initializeData();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_inc_monitor, container, false);
recycler_view = (RecyclerView) view.findViewById(R.id.fragment_inc_cards);
recycler_view.setHasFixedSize(true);
linear_layout_manager = new LinearLayoutManager(getActivity().getApplicationContext());
recycler_view.setLayoutManager(linear_layout_manager);
inc_adapter = new INCAdapter(inc_cards);
recycler_view.setAdapter(inc_adapter);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
但它只显示了这一点:
为什么它不识别颜色? R.drawable.....
如何被onBindViewHolder
而不是R.color.....
识别出来?
答案 0 :(得分:9)
更改
cardViewHolder.card_view.setCardBackgroundColor(inc_cards.get(i).inc_status);
到
int colorId = inc_cards.get(i).inc_status;
int color = cardViewHolder.card_view.getContext().getResources().getColor(colorId);
cardViewHolder.card_view.setCardBackgroundColor(color);
您使用的是R.color中的值,而不是您在XML中设置的值。
答案 1 :(得分:0)
我得到了更可靠的颜色:
setBackgroundTintList(ColorStateList.valueOf(颜色));
而不是setCardBackgroundColor(color)。
答案 2 :(得分:0)
要添加的一件事,请确保您在颜色编号中包含Alpha,并且如果您不只是在颜色的开头添加ff
,否则它将无法正常工作。
例如,这可行
view.setCardBackgroundColor(0xff2ecc71)
虽然这个显示白色背景
view.setCardBackgroundColor(0x2ecc71)