Dyanamic tabs in android with dynamic content from json -
i m creating dynamic tabs in android in data tabs populated json
following json response
{ "shop_details": [ { "id": "36", "shop_name": "all in 1 mart", "shop_no": "23223", "shop_address": "tinkune", "phone": "9804966595", "email": "arjundangal4@gmail.com", "user_name": "arjun", "address": "", "tel": "", "fax": "", "facebook": "", "twitter": "", "googleplus": "", "image": "", "featured_image": "" } ], "category": [ { "category_id": "35", "category_name": "skirt", "product": [ { "product_id": "49", "product_name": "skirt", "category_id": "35", "subcategory_id": "37", "product_color": "blue", "description": "skirt girls", "price": "301", "discount": "0", "service_charge": "0", "user_id": "36", "image_name": "6baab8a5308b7e821f5b6387794979a4.jpeg", "created_at": "2016-07-04 03:54:54" } ] }, { "category_id": "36", "category_name": "men", "product": [ { "product_id": "48", "product_name": "glasses", "category_id": "36", "subcategory_id": "39", "product_color": "red", "description": "glasses of rayban", "price": "594", "discount": "23", "service_charge": "22", "user_id": "36", "image_name": "fce01420a9021fdb159226b4bdc5b591.jpg", "created_at": "2016-07-04 03:52:58" } ] }, { "category_id": "37", "category_name": "bags", "product": [ { "product_id": "50", "product_name": "laptop bag", "category_id": "37", "subcategory_id": "41", "product_color": "black", "description": "bag carry laptop", "price": "190", "discount": "2", "service_charge": "3", "user_id": "36", "image_name": "e836e090a54cd2b6b594fa0a3382bb38.jpg", "created_at": "2016-07-04 04:14:08" } ] } ] }
following code fetch in add tabs dynamically
private void getshopdetails() { coordinatorlayout.setvisibility(view.gone); new progressdialog(this); progressdialog.show(); jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(shopdetailsjsonurl, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { progressdialog.dismiss(); coordinatorlayout.setvisibility(view.visible); try { jsonarray shopdetailsarray = response.getjsonarray("shop_details"); jsonobject shopdetailsobj = shopdetailsarray.getjsonobject(0); shopname = shopdetailsobj.getstring("shop_name"); phone = shopdetailsobj.getstring("tel"); shopnum = shopdetailsobj.getstring("shop_no"); shopaddress = shopdetailsobj.getstring("shop_address"); shopimage = shopdetailsobj.getstring("featured_image"); glide.with(getapplicationcontext()).load("http://allmartapp.com/appapi/uploads/" + shopimage).diskcachestrategy(diskcachestrategy.source).into(backdrop); collayout.settitle(shopname); address.settext("address - shop no -" + shopnum + ", " + shopaddress); jsonarray tabsarray = response.getjsonarray("category"); categoryid = new int[tabsarray.length()]; (int = 0; < tabsarray.length(); i++) { jsonobject tabsobj = tabsarray.getjsonobject(i); tablayout.addtab(tablayout.newtab().settext(tabsobj.getstring("category_name"))); categoryid[i] = tabsobj.getint("category_id"); } tablayout.settabgravity(tablayout.gravity_fill); pageradapter pageradapter = new pageradapter(getsupportfragmentmanager(), tablayout.gettabcount()); pager.setadapter(pageradapter); pager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { pager.setcurrentitem(tab.getposition()); shopdetailstabsfragment.sgetid(categoryid[tab.getposition()]); log.d("catiid", categoryid[tab.getposition()] + ""); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { progressdialog.dismisswitherror(); } }); int sockettimeout = 30000;//30 seconds - change want retrypolicy policy = new defaultretrypolicy(sockettimeout, defaultretrypolicy.default_max_retries, defaultretrypolicy.default_backoff_mult); jsonobjectrequest.setretrypolicy(policy); appcontroller.getinstance().addtorequestqueue(jsonobjectrequest); }
in ontabselected() assigned category id according position of tabs , call sgetid() function in fragment
my pageradapter returns 1 fragment recyclerview. following fragment code
public class shopdetailstabsfragment extends fragment { recyclerview recyclerview; boolean isviewshown = false; categorylistitemsadapter shoplistrvadapters; private string shopdetailsjsonurl = "http://allmartapp.com/appapi/json/get_shop_details_by_shop_id/"; private string baseshopdetailsjsonurl = "http://allmartapp.com/appapi/json/get_shop_details_by_shop_id/"; arraylist<categoryitemslistmodel> arraylist = new arraylist<>(); static int catid = 0; int shopid; public shopdetailstabsfragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v = inflater.inflate(r.layout.fragment_shop_details_tabs, container, false); recyclerview = (recyclerview) v.findviewbyid(r.id.rv); recyclerview.setlayoutmanager(new gridlayoutmanager(getactivity(), 2)); sharedpreferences sharedpreferences = getactivity().getsharedpreferences("shopid", context.mode_private); shopid = sharedpreferences.getint("shopid", 0); return v; } @override public void setuservisiblehint(boolean isvisibletouser) { super.setuservisiblehint(isvisibletouser); if (getview() != null) { isviewshown = true; getshopcat(); // fetchdata() contains logic show data when page selected asynctask fill data } else { isviewshown = false; } } public static void sgetid(int cat) { catid = cat; } private void getshopcat() { recyclerview.setadapter(null); shopdetailsjsonurl += shopid; arraylist.clear(); jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(shopdetailsjsonurl, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { log.d("json", response.tostring()); try { jsonarray categoryarray = response.getjsonarray("category"); (int = 0; < categoryarray.length(); i++) { jsonobject catobj = categoryarray.getjsonobject(i); int category_id = catobj.getint("category_id"); if (category_id == catid) { jsonarray productarray = catobj.getjsonarray("product"); (int j = 0; j < productarray.length(); j++) { jsonobject productobj = productarray.getjsonobject(j); string name = productobj.getstring("product_name"); string image = productobj.getstring("image_name"); int id = productobj.getint("product_id"); int price = productobj.getint("product_id"); categoryitemslistmodel shoplistrvmodels = new categoryitemslistmodel(id, image, name, price); arraylist.add(shoplistrvmodels); shoplistrvadapters = new categorylistitemsadapter(arraylist, getactivity()); recyclerview.setadapter(shoplistrvadapters); shoplistrvadapters.notifydatasetchanged(); } } } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); int sockettimeout = 30000;//30 seconds - change want retrypolicy policy = new defaultretrypolicy(sockettimeout, defaultretrypolicy.default_max_retries, defaultretrypolicy.default_backoff_mult); jsonobjectrequest.setretrypolicy(policy); appcontroller.getinstance().addtorequestqueue(jsonobjectrequest); shopdetailsjsonurl = baseshopdetailsjsonurl; } }
my problem that, tabs showing strange behaviors. know logic fetch category lists correct. think there problem in state of fragment. when switch tabs, data loaded in tabs duplicated , data loaded twice sometimes. suggestions appreciated im facing problem couple of days. want know easy way load data in tabs.
try below code if helps:
public class shopdetailstabsfragment extends fragment { recyclerview recyclerview; boolean isviewshown = false; categorylistitemsadapter shoplistrvadapters; private string shopdetailsjsonurl = "http://allmartapp.com/appapi/json/get_shop_details_by_shop_id/"; private string baseshopdetailsjsonurl = "http://allmartapp.com/appapi/json/get_shop_details_by_shop_id/"; arraylist<categoryitemslistmodel> arraylist = new arraylist<>(); static int catid = 0; int shopid; public shopdetailstabsfragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v = inflater.inflate(r.layout.fragment_shop_details_tabs, container, false); recyclerview = (recyclerview) v.findviewbyid(r.id.rv); recyclerview.setlayoutmanager(new gridlayoutmanager(getactivity(), 2)); sharedpreferences sharedpreferences = getactivity().getsharedpreferences("shopid", context.mode_private); shopid = sharedpreferences.getint("shopid", 0); shoplistrvadapters = new categorylistitemsadapter(arraylist, getactivity()); recyclerview.setadapter(shoplistrvadapters); return v; } @override public void setuservisiblehint(boolean isvisibletouser) { super.setuservisiblehint(isvisibletouser); if (getview() != null) { isviewshown = true; getshopcat(); // fetchdata() contains logic show data when page selected asynctask fill data } else { isviewshown = false; } } public static void sgetid(int cat) { catid = cat; } private void getshopcat() { recyclerview.setadapter(null); shopdetailsjsonurl += shopid; arraylist.clear(); jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(shopdetailsjsonurl, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { log.d("json", response.tostring()); try { jsonarray categoryarray = response.getjsonarray("category"); if(arraylist!=null){ if(arraylist.size()>0) arraylist.clear(); } (int = 0; < categoryarray.length(); i++) { jsonobject catobj = categoryarray.getjsonobject(i); int category_id = catobj.getint("category_id"); if (category_id == catid) { jsonarray productarray = catobj.getjsonarray("product"); (int j = 0; j < productarray.length(); j++) { jsonobject productobj = productarray.getjsonobject(j); string name = productobj.getstring("product_name"); string image = productobj.getstring("image_name"); int id = productobj.getint("product_id"); int price = productobj.getint("product_id"); arraylist.add(new categoryitemslistmodel(id, image, name, price)); } shoplistrvadapters.notifydatasetchanged(); } } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); int sockettimeout = 30000;//30 seconds - change want retrypolicy policy = new defaultretrypolicy(sockettimeout, defaultretrypolicy.default_max_retries, defaultretrypolicy.default_backoff_mult); jsonobjectrequest.setretrypolicy(policy); appcontroller.getinstance().addtorequestqueue(jsonobjectrequest); shopdetailsjsonurl = baseshopdetailsjsonurl; }
Comments
Post a Comment