How to Inflate new layout inside cardview dynamically - android

I am trying to inflate the second XML inside cardview dynamically, but it throws null pointer exception.When I am trying to do hard code(by including directly inside card view layout) it works well.Is there any other way to inflate new XML dynamically?
CardView Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/room_detail_cards"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/room_detail_Linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/module_type_5"/>
<!--<include layout="#layout/module_type_3"/>-->
<!--<It Works well when i hard code the layout here/>-->
</LinearLayout>
</android.support.v7.widget.CardView>
RecyclerAdapter.java(onCreateViewHolder)
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout);
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.module_type_3, layout, false);
layout.addView(itemView); //NULL Pointer Exception occurs here
View itemView1 = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.room_detail_card_view, null, false);
return new ViewHolder(itemView1);
}
First layout XML(module_type_5)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:hint="module"
android:id="#+id/module_5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Second XML Layout(module_type_3)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:hint="module"
android:id="#+id/module_5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

You are trying to get view id before inflating it at here:
layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout);
That's why layout is null.
Use onCreateViewHolder to return an instance of ViewHolder and perform all assignments in the view holder.
Something like this would work:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.room_detail_card_view, parent, false));
}
And then in your ViewHolder class:
public class ViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.textView);
}
}
And finally data binding in onBindViewHolder:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText("some data")
}

Related

Change RecycleView item width?

I know there is probably a very simple solution out there, but for the life of me I can't seem to find it. How do I change the width of the RecycleView item? For some reason, it's displaying the first item full width of the parent... It's displaying a lot of white space in between each item.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_wrapper"
android:background="#color/colorPrimary"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cats"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
CategoryAdapter:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private ArrayList<Category> categories;
public CategoryAdapter(ArrayList<Category> categories) {
this.categories = categories;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cat, parent, false);
return new ViewHolder(v);
}
#Override public void onBindViewHolder(ViewHolder holder, int position) {
Category cat = this.categories.get(position);
holder.text.setText(cat.getText().toString());
}
#Override public int getItemCount() {
return categories.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.cat_title);
}
}
}
single_cat.xml
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Cat"
android:background="#drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="#+id/cat_title"
android:textSize="13sp"
android:fontFamily="#font/open_sans_bold"
android:textColor="#color/colorPrimary"/>
1)in your fragment do :
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, true));
2)for recyclerView single item .xml do :
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>
3) and change RecyclerView to :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_wrapper"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:id="#+id/cats"/>
</LinearLayout>
i would change single cat to
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="#+id/cat_title"
android:textSize="13sp"
android:fontFamily="#font/open_sans_bold"
android:textColor="#color/colorPrimary"/>
Finally, after 2 days and looking all over the internet, I figured it out myself.
Make sure your Constraint Layout is wrap_content. This was the issue all along!

RecycleView does not scroll to the last items

I have a List View with Vertical Scroll, its item contains RecyclerView which will scroll Horizontally.
But when i scroll horizontal, RecyclerView does not allow me to scroll to the last item in the list (the picture below). What's happened with it? Even, i forced it scroll to the last item by setting linearLayoutManager.setStackFromEnd(true); it still does not work, only scroll to half of the 8th item, not the last item in list of 10
Edit: add codes:
list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.phongponix.trackingbodybuilding.MainActivity$PlaceholderFragment">
<ListView android:id="#+id/lvExerciseList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="50dp"
></ListView>
</RelativeLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="20dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imgExcercisePhoto"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView android:text="aaa"
android:id="#+id/tvExerciseName"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvExcerciseRecords"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:layout_margin="10dp"
android:paddingRight="50dp"
android:scrollbars="horizontal">
</android.support.v7.widget.RecyclerView>
</TableRow>
</TableLayout>
</LinearLayout>
list_item_horizontal.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dp"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingRight="10dp">
<TextView
android:id="#+id/tvTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Code for List Adapter:
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
view.setTag(viewHolder);
recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(trackingPlanHorizontalAdapter);
recyclerView.setNestedScrollingEnabled(false);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (data.size() > 0) {
ExerciseModel exercise = (ExerciseModel) data.get(i);
viewHolder.title.setText(exercise.getTitle());
} else {
viewHolder.title.setText("No data");
}
return view;
}
Code for Recycle Adapter:
public TrackingPlanHorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_horizontal, parent, false);
return new TrackingPlanHorizontalViewHolder(itemView);
}
#Override
public void onBindViewHolder(TrackingPlanHorizontalViewHolder holder, int position) {
holder.textView.setText(horizontalList.get(position));
}
Yes i found your problem,there is problem regarding your list_item.xml change your list item with below code,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<LinearLayout
android:id="#+id/first_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:src="#mipmap/ic_launcher"
android:id="#+id/imgExcercisePhoto"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="#+id/tvExerciseName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="aaa" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_toRightOf="#+id/first_panel"
android:id="#+id/rvExcerciseRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

Row item in recycler view does not respond to click event cardview

I have a RecyclerView with CardView the problem is that the click doesn't repsond(unless i press the bottom of the view).I have set the clickable="true"
but it doesn't help
this is my row_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
android:clickable="true"
card_view:cardUseCompatPadding="true" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:foreground="?selectableItemBackground"
android:clickable="true">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_name"
android:gravity="center"
android:padding="10dp"
android:textColor="#color/material_deep_teal_200"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cb_check_box"
android:gravity="right"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="number"
android:clickable="true"
android:id="#+id/tv_number"
android:layout_below="#+id/tv_name"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is how i set the onClick:
// Create new views (invoked by the layout manager)
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.contacts_list_item, parent, false);
// set the view's size, margins, paddings and layout parameters
v.setOnClickListener(this);
CustomViewHolder vh = new CustomViewHolder(v);
return vh;
}

I can use cardflip effect in a recyclerview item?

I want use the card flip effect (http://developer.android.com/intl/es/training/animation/cardflip.html) in each item of a recyclerview.
I have the 3 views.
item_cancion:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
item_cancion_front and item_cancion_back (Two xml with the same info for a proof)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgReproduciendo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/txtArtista"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:maxWidth="30dp"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/txtTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:singleLine="true"
android:layout_toLeftOf="#+id/txtDuracion"
android:layout_toRightOf="#+id/imgReproduciendo"
android:layout_toEndOf="#+id/imgReproduciendo"
android:text="Titulo" />
<TextView
android:id="#+id/txtArtista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:singleLine="true"
android:maxWidth="190dp"
android:layout_below="#+id/txtTitulo"
android:layout_toRightOf="#+id/imgReproduciendo"
android:layout_toEndOf="#+id/imgReproduciendo"
android:text="Artista" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtAlbum"
android:singleLine="true"
android:textSize="14dp"
android:gravity="right"
android:layout_below="#+id/txtDuracion"
android:layout_alignRight="#+id/txtDuracion"
android:layout_alignEnd="#+id/txtDuracion"
android:layout_toRightOf="#+id/txtArtista"
android:layout_marginLeft="15dp"
android:text="Album" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtDuracion"
android:paddingLeft="25dp"
android:singleLine="true"
android:textSize="14dp"
android:textStyle="italic"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="Duracion" />
</RelativeLayout>
In the viewHolder adapter I have:
#Override
public AdaptadorCancionesActual.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cancion, parent, false);
actividad.getFragmentManager().beginTransaction().add(R.id.container, new CardFrontFragment()).commit();
ViewHolder vh = new ViewHolder(v, this);
return vh;
}
and the ViewHolder is:
public ViewHolder(View v, AdaptadorCancionesActual mAdapter) {
super(v);
this.mAdapter = mAdapter;
frameLayout = (RelativeLayout) v.findViewById(R.id.container);
txtCancion = (TextView) frameLayout.findViewById(R.id.txtTitulo);
txtArtista = (TextView) frameLayout.findViewById(R.id.txtArtista);
txtAlbum = (TextView) frameLayout.findViewById(R.id.txtAlbum);
txtFin = (TextView) frameLayout.findViewById(R.id.txtDuracion);
imgReproduciendo = (ImageView) frameLayout.findViewById(R.id.imgReproduciendo);
v.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int position = getLayoutPosition();
Datos.getMusicSrv().setSong(position);
Datos.getMusicSrv().playSong();
mAdapter.flipCard();
//mAdapter.notifyDataSetChanged();
}
I have two question...
First, I can use this effect in the item of recyclerView?
And if Yes... Why the TextViews are null.

Zebra-striping color-effect on RecycledView widget rows

What I'm trying to do is to assign a different color to every other row in a RecycledView. I created a custom RecycledView.Adapter, and in onCreateViewHolder method I have this:
// Create a row_folder view
View view = inflater.inflate(R.layout.row_folder, parent, false);
if(position % 2 == 0) {
view.setBackgroundColor(Color.BLACK);
}
// Use MyViewHolder to bind the view
MyViewHolder holder = new MyViewHolder(view);
return holder;
The color does not get assigned. What am I missing ? Thanks.
edited:
row_folder xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="#color/my_white"
android:orientation="horizontal">
<ImageView
android:id="#+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="#drawable/folder_icon" />
<TextView
android:id="#+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="#drawable/folder_content_icon" />
<TextView
android:id="#+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
do something like this in your recycler view adapter:
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
if(position % 2 == 0)
{
//holder.rootView.setBackgroundColor(Color.BLACK);
holder.rootView.setBackgroundResource(R.color.black);
}
else
{
//holder.rootView.setBackgroundColor(Color.WHITE);
holder.rootView.setBackgroundResource(R.color.white);
}
}
EDIT:
Change your xml file like this: row_folder.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="#color/my_white"
android:orientation="horizontal">
<ImageView
android:id="#+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="#drawable/folder_icon" />
<TextView
android:id="#+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="#drawable/folder_content_icon" />
<TextView
android:id="#+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
create one more variable in your ViewHolder class as below:
public static class ViewHolder extends RecyclerView.ViewHolder
{
LinearLayout rootView;//newly added field
public ViewHolder(View view)
{
super(view);
rootView=(LinearLayout)view.findViewById(R.id.rootView);
}
}
I hope it might work.
I'm putting this here just for future reference for those trying to apply a zebra striping like color effect on RecyclerView's row which basically holds a CardView.
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!--Content goes here-->
</android.support.v7.widget.CardView>
Now, in adapter side, initialize the CardView first (I'm using butterknife binding).
public class YooViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.cv)
CardView cardView;
public YooViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);
}
}
And finally at onBindViewHolder, place this single line of code.
holder.cardView.setCardBackgroundColor(
ContextCompat.getColor(mContext,
position % 2 == 0 ? R.color.white : R.color.white_grayish));
Be sure to use setCardBackgroundColor (and not setBackgroundColor) on CardView.

Categories

Resources