Android的ViewHolder背景颜色 [英] Android ViewHolder Background Color
问题描述
我创建包含嵌套CardView一个RecyclerView Android应用程序。我需要每隔卡交替到不同的颜色。我使用 @覆盖
覆盖 onBindViewHolder(ViewHolder VH,诠释POS)
方法。我需要从方法(我假设),改变背景颜色,但有没有方法来设置ViewHolder!的背景颜色
我为我noobie岬对不起,我正在学习Android开发了。
-Ben
编辑:code:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_9); pieRecyclerView =(RecyclerView)findViewById(R.id.recyclerView);
pieRecyclerView.setHasFixedSize(真);
馅饼= makePies(); pieLayoutManager =新LinearLayoutManager(本);
pieRecyclerView.setLayoutManager(pieLayoutManager);
PieAdapter适配器=新PieAdapter(馅饼);
pieRecyclerView.setAdapter(适配器);
}公共类PieAdapter扩展RecyclerView.Adapter< ViewHolder> { 上下文mContext;
ArrayList的<&馅饼GT; mPies;
LayoutInflater mInflater; 公共PieAdapter(ArrayList的<&馅饼GT;馅饼){ mPies =馅饼; } @覆盖
公共ViewHolder onCreateViewHolder(ViewGroup中的父母,INT viewType){ 视图V = LayoutInflater.from(parent.getContext())膨胀(R.layout.card_layout,父母,假的)。
返回新ViewHolder(V); } @覆盖
公共无效onBindViewHolder(ViewHolder持有人,INT位置){ 派currentPie = mPies.get(位置);
holder.textViewName.setText(currentPie.mName);
holder.textViewDescription.setText(currentPie.mDescription);
NumberFormat的格式化= NumberFormat.getCurrencyInstance();
串价格= formatter.format(currentPie.mPrice);
holder.textViewPrice.setText(价); } @覆盖
公众诠释getItemCount(){ 返回mPies.size(); }
}私有类ViewHolder扩展RecyclerView.ViewHolder { 公众的TextView textViewName;
公众的TextView textViewDescription;
公众的TextView textViewPrice; 公共ViewHolder(视图v){ 超级(五);
textViewName =(TextView中)v.findViewById(R.id.textViewName);
textViewDescription =(TextView中)v.findViewById(R.id.textViewDescription);
textViewPrice =(TextView中)v.findViewById(R.id.textViewPrice); }
}私人的ArrayList<&馅饼GT; makePies(){ ArrayList的<&馅饼GT;馅饼=新的ArrayList<&馅饼GT;();
pies.add(新派(苹果,一个老fashoned的最爱。,1.5));
pies.add(新派(蓝莓,使用新鲜蓝莓缅因州,1.5));
pies.add(新派(樱桃,美味和每日新鲜制造,2.0));
pies.add(新派(椰奶油,顾客的喜爱。2.5));
返回馅饼;}私有类馅饼{ 字符串MNAME;
串mDescription;
双mPrice; 公共饼(字符串名称,字符串描述,双价){ this.mName =名称;
this.mDescription =描述;
this.mPrice =价格; }
}`
的 ViewHolder
对象本身不是查看
。如果你想改变整个列表项的背景,你可能想调用 viewHolder.itemView.setBackgroundColor(...)
。在 ItemView控件
的 ViewHolder
是什么查看
传递进构造函数。
I am creating an Android application that contains a RecyclerView with a nested CardView. I need to alternate every other Card to a different color. I am using @Override
to override the onBindViewHolder(ViewHolder vh, int pos)
method. I need to change the background color from that method (I assume), but there are no methods to set the background color of a ViewHolder!
I'm sorry for my noobie-ness, I'm learning Android development now.
-Ben
EDIT: Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_9);
pieRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
pieRecyclerView.setHasFixedSize(true);
pies = makePies();
pieLayoutManager = new LinearLayoutManager(this);
pieRecyclerView.setLayoutManager(pieLayoutManager);
PieAdapter adapter = new PieAdapter(pies);
pieRecyclerView.setAdapter(adapter);
}
public class PieAdapter extends RecyclerView.Adapter<ViewHolder> {
Context mContext;
ArrayList<Pie> mPies;
LayoutInflater mInflater;
public PieAdapter(ArrayList<Pie> pies) {
mPies = pies;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Pie currentPie = mPies.get(position);
holder.textViewName.setText(currentPie.mName);
holder.textViewDescription.setText(currentPie.mDescription);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String price = formatter.format(currentPie.mPrice);
holder.textViewPrice.setText(price);
}
@Override
public int getItemCount() {
return mPies.size();
}
}
private class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public TextView textViewDescription;
public TextView textViewPrice;
public ViewHolder(View v) {
super(v);
textViewName = (TextView) v.findViewById(R.id.textViewName);
textViewDescription = (TextView) v.findViewById(R.id.textViewDescription);
textViewPrice = (TextView) v.findViewById(R.id.textViewPrice);
}
}
private ArrayList<Pie> makePies() {
ArrayList<Pie> pies = new ArrayList<Pie>();
pies.add(new Pie("Apple", "An old-fashoned favorite.", 1.5));
pies.add(new Pie("Blueberry", "Made with fresh Maine blueberries.", 1.5));
pies.add(new Pie("Cherry", "Delicious and fresh made daily", 2.0));
pies.add(new Pie("Coconut Cream", "A customer favorite.", 2.5));
return pies;
}
private class Pie {
String mName;
String mDescription;
double mPrice;
public Pie(String name, String description, double price) {
this.mName = name;
this.mDescription = description;
this.mPrice = price;
}
}`
The ViewHolder
object is not itself a View
. If you want to change the background of the entire list item, you probably want to call viewHolder.itemView.setBackgroundColor(...)
. The itemView
of a ViewHolder
is whatever View
you passed into the constructor.
这篇关于Android的ViewHolder背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!