android - Tablayout with ViewPager inside RecyclerView -
i have activity dynamic tabs.each tab contain same recyclerview.i able achieve .what want each item of recyclerview in each tab have own tab layout viewpager. creating single fragment , passing 3 tabs different data inside each tab. problem first item of recyclerview shows data remaining item either doesn't show data @ or in shows wrong data sometimes.can please me this.have been stuck 2 days.i know don't have proper credit still can 1 please help.
my recycler view adpater code..
import android.app.activity; import android.content.context; import android.os.bundle; import android.support.design.widget.bottomsheetdialog; import android.support.design.widget.tablayout; import android.support.transition.transitionmanager; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.app.fragmentstatepageradapter; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.cardview; import android.support.v7.widget.defaultitemanimator; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.util.log; import android.view.layoutinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.view.animation.animation; import android.view.animation.animationset; import android.view.animation.animationutils; import android.view.animation.decelerateinterpolator; import android.view.animation.rotateanimation; import android.widget.button; import android.widget.imagebutton; import android.widget.imageview; import android.widget.linearlayout; import android.widget.popupmenu; import android.widget.relativelayout; import android.widget.textview; import com.bumptech.glide.glide; import com.bumptech.glide.load.engine.diskcachestrategy; import com.fitcarte.fitcarte.itemdescriptiontabfragment; import com.fitcarte.fitcarte.fitcarte.r; import com.fitcarte.fitcarte.sqlite.mealconfiguratordbhandler; import com.fitcarte.fitcarte.timepicker.timepickerpopwin; import java.util.arraylist; import java.util.list; /** * custom adapter mealplanner recycler view */ public class mealplanneradapter extends recyclerview.adapter<mealplanneradapter.myviewholder> implements changemealclicklistener { public interface mealplannerinerface { void mealaddress(int position); } private list<mealplanner> mmealplanner = new arraylist<>(); private static context mcontext; private bottomsheetdialog mbottomsheet; private mealconfiguratordbhandler mealconfiguratordbhandler; private int mposition; private fragmentmanager parentfragment; private mealplannerinerface minterface; public static class myviewholder extends recyclerview.viewholder { private final cardview mcardview; private final linearlayout mlinearlayout; private final imageview mmealimage, mmealcategory; private final textview mmealname, mmealtype, mmealorder, mmealcalorie, mmealprice, mdeliverytime, mdeliveryaddresslabel; private final imagebutton moptionmenu, mmealdescriptionarrow; private final linearlayout mmealdescriptionlayout; private final button maddmeal; private final tablayout mtablayout; private final viewpager mviewpager; public myviewholder(view view) { super(view); mcardview = (cardview) view.findviewbyid(r.id.mealplannercardview); mlinearlayout = (linearlayout) view.findviewbyid(r.id.mealplanneraddmeallayout); mtablayout = (tablayout) view.findviewbyid(r.id.mealplannertab); mviewpager = (viewpager) view.findviewbyid(r.id.mealplannerpager); mmealimage = (imageview) view.findviewbyid(r.id.mealplannermealimage); mmealcategory = (imageview) view.findviewbyid(r.id.mealplannermealcategory); mmealname = (textview) view.findviewbyid(r.id.mealplannermealname); mmealtype = (textview) view.findviewbyid(r.id.mealplannermealtype); mmealorder = (textview) view.findviewbyid(r.id.mealplannermealorder); mmealcalorie = (textview) view.findviewbyid(r.id.mealplannermealcalorie); mmealprice = (textview) view.findviewbyid(r.id.mealplannermealprice); mdeliverytime = (textview) view.findviewbyid(r.id.mealplannermealtime); mdeliveryaddresslabel = (textview) view.findviewbyid(r.id.mealplannermealaddresslabel); moptionmenu = (imagebutton) view.findviewbyid(r.id.mealoptionmenu); mmealdescriptionarrow = (imagebutton) view.findviewbyid(r.id.mealplannerdescriptionarrow); mmealdescriptionlayout = (linearlayout) view.findviewbyid(r.id.mealplannerdescriptionlayout); maddmeal = (button) view.findviewbyid(r.id.mealplanneraddmeal); } } public mealplanneradapter(list<mealplanner> mmealplanner, context mcontext, mealplannerinerface minterface,fragmentmanager parentfragment) { this.mmealplanner = mmealplanner; this.mcontext = mcontext; this.minterface = minterface; this.parentfragment=parentfragment; mealconfiguratordbhandler = new mealconfiguratordbhandler(mcontext); } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view itemview = layoutinflater.from(mcontext).inflate(r.layout.layout_meal_planner_card, parent, false); return new myviewholder(itemview); } @override public void onbindviewholder(final myviewholder holder, final int position) { holder.mtablayout.setupwithviewpager(holder.mviewpager); holder.mtablayout.setfillviewport(true); setupviewpager( holder.mviewpager, holder.mtablayout, position); string mealtype = mmealplanner.get(position).getmealtype() + " meal - "; string calorie = mmealplanner.get(position).getmealcalorie() + " kcal"; string price = mcontext.getresources().getstring(r.string.rs) + " " + mmealplanner.get(position).getmealprice(); glide.with(mcontext) .load(mmealplanner.get(position).getmealimage()) .centercrop() .placeholder(r.drawable.placeholder) .diskcachestrategy(diskcachestrategy.source) .into(holder.mmealimage); holder.mmealname.settext(mmealplanner.get(position).getmealname()); holder.mmealtype.settext(mealtype); holder.mmealorder.settext(mmealplanner.get(position).getmealtime()); if (mmealplanner.get(position).getmealcategory().equalsignorecase("veg")) { holder.mmealcategory.setimageresource(r.drawable.ic_veg); } else { holder.mmealcategory.setimageresource(r.drawable.ic_nonveg); } holder.mmealcalorie.settext(calorie); holder.mmealprice.settext(price); holder.mdeliverytime.settext(mmealplanner.get(position).getdeliverytime()); holder.mdeliveryaddresslabel.settext(mmealplanner.get(position).getdeliveryaddresslabel()); } @override public int getitemcount() { return mmealplanner.size(); } private void setupviewpager(viewpager mviewpager,tablayout mtablayout,int mposition) { viewpageradapter adapter = new viewpageradapter(parentfragment); //add ingredients fragment fragment fragment_ingredients = new mealplannertabfragment(); bundle bundle_ingredients = new bundle(); bundle_ingredients.putstring("type", "tab_ingredients"); bundle_ingredients.putstring("ingredients", mmealplanner.get(mposition).getmealname()); fragment_ingredients.setarguments(bundle_ingredients); //add nutritional facts fragment fragment fragment_nutritional_facts = new mealplannertabfragment(); bundle bundle_calories = new bundle(); bundle_calories.putstring("type", "tab_nutritional_facts"); bundle_calories.putstring("calory", mmealplanner.get(mposition).getmealcalorie()); bundle_calories.putstring("protein", mmealplanner.get(mposition).getmealprotein()); bundle_calories.putstring("carbs", mmealplanner.get(mposition).getmealcarbs()); bundle_calories.putstring("fats", mmealplanner.get(mposition).getmealfat()); fragment_nutritional_facts.setarguments(bundle_calories); //add ingredients fragment fragment fragment_health_benefits = new mealplannertabfragment(); bundle bundle_time = new bundle(); bundle_time.putstring("type", "health_benefits"); fragment_health_benefits.setarguments(bundle_time); adapter.addfragment(fragment_ingredients, "ingredients"); adapter.addfragment(fragment_nutritional_facts, "nutritional facts"); adapter.addfragment(fragment_health_benefits, "health benefits"); mviewpager.setadapter(adapter); (int = 0; < mtablayout.gettabcount(); i++) { mtablayout.gettabat(i).setcustomview(adapter.gettabview(i)); } } /** * viewpageradapter class tab layout */ private static class viewpageradapter extends fragmentstatepageradapter{ private final list<fragment> mfragmentlist = new arraylist<>(); private final list<string> mfragmenttitlelist = new arraylist<>(); public viewpageradapter(fragmentmanager manager) { super(manager); } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getcount() { return mfragmentlist.size(); } public void addfragment(fragment fragment, string title) { mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } @override public charsequence getpagetitle(int position) { return mfragmenttitlelist.get(position); } public view gettabview(int i) { view view = layoutinflater.from(mcontext).inflate(r.layout.layout_custom_tab, null); textview txt = (textview) view.findviewbyid(r.id.tab); if (i == 0) { txt.settext("ingredients"); txt.setcompounddrawableswithintrinsicbounds(0, r.drawable.selector_ingrediants, 0, 0); } else if (i == 1) { txt.settext("nutritional facts"); txt.setcompounddrawableswithintrinsicbounds(0, r.drawable.selector_nutritional_facts, 0, 0); } else { txt.settext("health benefits"); txt.setcompounddrawableswithintrinsicbounds(0, r.drawable.selector_health_benefits, 0, 0); } return view; } } }
fragment code:
import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.progressbar; import android.widget.relativelayout; import android.widget.textview; /** * simple {@link fragment} subclass. */ public class mealplannertabfragment extends fragment { private string type,calory,protein,carbs,fats,ingredients; private relativelayout ingredientslayout, nutritionalfactslayout, healthbenifitslayout; private textview ingredientstextview, caloriestextview, proteintextview, carbstextview, fatstextview; private progressbar progress; private int c=0; public mealplannertabfragment() { // required empty public constructor } @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null && c==0 ) { c=1; type = getarguments().getstring("type"); if (type.equalsignorecase("tab_nutritional_facts")) { calory = getarguments().getstring("calory") + " kcal"; protein = getarguments().getstring("protein") + " kcal"; carbs = getarguments().getstring("carbs") + " kcal"; fats = getarguments().getstring("fats") + " kcal"; }else if(type.equalsignorecase("tab_ingredients")){ ingredients=getarguments().getstring("ingredients"); } } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.layout_meal_planner_description_tab, container, false); ingredientslayout = (relativelayout) rootview.findviewbyid(r.id.ingredients_layout); nutritionalfactslayout = (relativelayout) rootview.findviewbyid(r.id.nutritional_facts_layout); healthbenifitslayout = (relativelayout) rootview.findviewbyid(r.id.health_benefits_layout); ingredientstextview = (textview) rootview.findviewbyid(r.id.id_txt); caloriestextview = (textview) rootview.findviewbyid(r.id.txt_calories); proteintextview = (textview) rootview.findviewbyid(r.id.txt_protein); carbstextview = (textview) rootview.findviewbyid(r.id.txt_carbs); fatstextview = (textview) rootview.findviewbyid(r.id.txt_fats); progress = (progressbar) rootview.findviewbyid(r.id.progress); if (type.equalsignorecase("tab_nutritional_facts")) { ingredientslayout.setvisibility(view.gone); nutritionalfactslayout.setvisibility(view.visible); healthbenifitslayout.setvisibility(view.gone); caloriestextview.settext(calory); proteintextview.settext(protein); carbstextview.settext(carbs); fatstextview.settext(fats); }else if(type.equalsignorecase("tab_ingredients")){ ingredientslayout.setvisibility(view.visible); nutritionalfactslayout.setvisibility(view.gone); healthbenifitslayout.setvisibility(view.gone); ingredientstextview.settext(ingredients); } else { ingredientslayout.setvisibility(view.gone); nutritionalfactslayout.setvisibility(view.gone); healthbenifitslayout.setvisibility(view.visible); } return rootview; } }
Comments
Post a Comment