Android ViewHolder Background Color

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

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;      } }` 

回答1:

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.



回答2:

Note that RecyclerView.Adapter is a generic class defined thusly:

public class RecyclerView.Adapter<VH extends android.support.v7.widget.RecyclerView.ViewHolder> 

You will create your own subclass of ViewHolder that has private variables for references to the child views in your item view. Then you will define your adapter subclass something like this:

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> 

The idea is that during onCreateViewHolder() when you create your ViewHolder subclass, you will also inflate a view and store references to the child views in private variables. That's what makes it a view holder, you get and store references to all the child views.

Then, when onBindViewHolder() is called, you get the position to determine the data to put into those views.

Keep in mind that the RecyclerView only calls onCreateViewHolder() when it doesn't have any ViewHolders to recycle.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!