i'm wondering if it's possible change background colour of each recycler view item if item contains word. example, custom item contains 4 textviews , 1 checkbox, want background colour light brown if item contains word "dead", red if contains "bench".. ect ect.. there way this?
here item.xml:
<android.support.v7.widget.cardview android:id="@+id/card_view" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="match_parent" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_margin="5dp" card_view:cardcornerradius="10dp" card_view:cardelevation="10dp" > <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingtop="5dp" android:paddingleft="5dp" android:paddingright="5dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="squat" android:textsize="20sp" android:paddingstart="5dp" android:paddingleft="5dp" android:paddingright="50dp" android:id="@+id/txtexercise" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="%" android:textsize="20sp" android:paddingleft="10dp" android:paddingright="10dp" android:id="@+id/txtpercentage" android:layout_centervertical="true" android:layout_toleftof="@+id/txtreps" android:layout_tostartof="@+id/txtreps"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="reps" android:paddingright="10dp" android:paddingleft="10dp" android:textsize="20sp" android:id="@+id/txtreps" android:layout_aligntop="@+id/check1" android:layout_toleftof="@+id/txtweight" android:layout_tostartof="@+id/txtweight"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="weight" android:paddingright="10dp" android:paddingleft="20dp" android:textsize="20sp" android:id="@+id/txtweight" android:layout_alignparenttop="true" android:layout_toleftof="@+id/check1" android:layout_tostartof="@+id/check1"/> <checkbox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/check1" android:paddingright="10dp" android:checked="false" android:layout_centervertical="true" android:layout_alignparentright="true" android:layout_alignparentend="true"/> </relativelayout>
and recycler view:
public class myrecycleradapter extends recyclerview.adapter<myviewholder> { context mcontext; arraylist<workout> workout; sharedpreferences prefs; int firstsecondorthird; public myrecycleradapter(context context, arraylist<workout> workout, int theposition) { mcontext = context; this.workout = workout; this.firstsecondorthird = theposition; } // initialize holder @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.workout_item, null); myviewholder holder = new myviewholder(view); return holder; } //bind data views @override public void onbindviewholder(myviewholder holder, final int position) { holder.exercise.settext(workout.get(position).getexercise()); holder.percent.settext(workout.get(position).getpercent()); holder.reps.settext(workout.get(position).getreps()); holder.weight.settext(workout.get(position).getweight()); holder.check1.setoncheckedchangelistener(null); final workout ischeck = workout.get(position); holder.check1.setchecked(ischeck.ischeck1()); prefs = mcontext.getsharedpreferences("checkstate", context.mode_private); holder.check1.setchecked(prefs.getboolean(firstsecondorthird+"checkstate"+position, false)); ischeck.setcheck1(prefs.getboolean(firstsecondorthird+"checkstate"+position, false)); holder.check1.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { ischeck.setcheck1(ischecked); prefs.edit().putboolean(firstsecondorthird+"checkstate"+position, ischecked).apply(); } }); } @override public int getitemcount() { return workout.size(); } }
thanks!
in base viewholder
class, itemview
public, in onbindviewholder()
method check color want , set it.
int colorresid = r.color.default; if (workout.get(position).getexercise().contains("bench") { colorresid = r.color.red; // more tests color use } int color = getresources.getcolor(colorresid, context); ((cardview) holder.itemview).setcardbackgroundcolor(color);
Comments
Post a Comment