java - How to refresh the fragment inside viewpager dynamically? -
i have 1 viewpager(mainviewpager) inside mainactivity renders fragment contains viewpager(imageviewpager) , textview. imageviewpager renders fragment contains imageview , button. i've set onclick listener on imageview turns in fullscreen view of image button beneath.now onclick of buttton want return mainactivity condition on same page entered , same image viewed last in fullscreen.i able same page image starts 0.
basically hierarchy like:
mainactivity --mainviewpager-->taskfragment --imageviewpager-->imagefragment(contains imageview --textview(set text returned mainactivity)
my code: mainactivity.java
public class mainactivity extends fragmentactivity { private viewpager mviewpager; private custompageradapter mpageradapter; private int changedposition; private string[] vehicles = new string[]{"audi q7", "honda accord", "hyundai i20", "maruti suzuki swift", "mahindra xuv 500", "swift dzire", "honda city", "honda civic", "mercedez benz", "rolls royce", "ferrari"}; private string[][] vehicle_info = new string[][]{ {"http://www.hdwallpapers.in/walls/2010_abt_audi_q7_3_tdi_3-wide.jpg", "http://www.hdwallpapers.in/walls/2010_mtm_audi_s5_cabrio_michelle_edition-wide.jpg", "http://www.hdwallpapers.in/walls/2009_audi_tts_coupe_car-wide.jpg"}, {"http://automobiles.honda.com/images/2015/accord-sedan/exterior-gallery/2015-honda-accord-sedan-sport-exterior-side1.jpg", "http://www.carprousa.com/wp-content/uploads/2015/01/hondainterior-e1421978263126.jpg", "http://www.carprousa.com/wp-content/uploads/2015/01/hondainterior2-e1421978365916.jpg"}, ........... }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mviewpager = (viewpager)findviewbyid(r.id.mainviewpager); mpageradapter = new custompageradapter(getsupportfragmentmanager(), vehicles, vehicle_info); mviewpager.setadapter(mpageradapter); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == 0) { changedposition = data.getintextra("changed_position", 0); log.i("position 3:"," "+changedposition); taskfragment fragment = new taskfragment(); bundle bundle = new bundle(); bundle.putint("image_position", changedposition); fragment.setarguments(bundle); } } }
activity_main.xml
<android.support.v4.view.viewpager android:id="@+id/mainviewpager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.viewpager>
custompageradapter.java
public class custompageradapter extends fragmentstatepageradapter{ private string[] vehicles,selected_vehicle_info; private string[][] vehicle_info; public custompageradapter(fragmentmanager fragmentmanager,string[] vehicles,string[][] vehicle_info){ super(fragmentmanager); this.vehicles = vehicles; this.vehicle_info = vehicle_info; } @override public fragment getitem(int position) { selected_vehicle_info = vehicle_info[position]; taskfragment taskfragment = new taskfragment(); bundle bundle = new bundle(); bundle.putstring("item",vehicles[position]); bundle.putstringarray("item_info",selected_vehicle_info); taskfragment.setarguments(bundle); return taskfragment; } @override public int getcount() { return vehicles.length; } }
taskfragment.java
public class taskfragment extends fragment { private string vehicle; private string[] vehicle_info; private viewpager imageviewpager; private imagepageradapter imagepageradapter; private int changedimageposition; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { vehicle = getarguments().getstring("item"); vehicle_info = getarguments().getstringarray("item_info"); view view = inflater.inflate(r.layout.activity_task_fragment,container,false); textview messagetextview = (textview) view.findviewbyid(r.id.textview); messagetextview.settext(vehicle); imageviewpager = (viewpager)view.findviewbyid(r.id.imageviewpager); imagepageradapter = new imagepageradapter(getactivity(), vehicle_info); imagepageradapter.notifydatasetchanged(); imageviewpager.setadapter(imagepageradapter); imageviewpager.setcurrentitem(changedimageposition); return view; } private class imagepageradapter extends pageradapter { private activity activity; private string[] vehicle_info; public imagepageradapter(activity activity,string[] vehicle_info){ this.activity = activity; this.vehicle_info = vehicle_info; } @override public object instantiateitem(viewgroup container, final int position) { layoutinflater inflater = layoutinflater.from(activity); view view = inflater.inflate(r.layout.activity_image_fragment,container,false); imageview imageview = (imageview) view.findviewbyid(r.id.imageview); picasso.with(getcontext()).load(vehicle_info[position]).into(imageview); if(imageview.getparent()!=null) ((viewgroup)imageview.getparent()).removeview(imageview); ((viewpager) container).addview(imageview, 0); imageview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(activity, imageinfullscreen.class); intent.putextra("images_url", vehicle_info); intent.putextra("image_position", position); activity.startactivityforresult(intent, 0); } }); return imageview; } @override public int getitemposition(object object) { return position_none; } @override public int getcount() { return vehicle_info.length; } @override public void destroyitem(viewgroup collection, int position, object view) { collection.removeview((view) view); } @override public boolean isviewfromobject(view view, object object) { return view == object; } } }
imageinfullscreen.java
button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent=new intent(); intent.putextra("changed_position",changedimgposition); setresult(0,intent);}});
//and same adapter n all.
where ever want refresh view put following code
this.page_position = 0; imagepageradapter.notifydatasetchanged();
and in imagepageradapter override following function -
@override public int getitemposition(object object) { return position_none; }
position_none means whenever notifydatasetchanged called, adapter create fresh views rather using same views.
this should work in our case.
Comments
Post a Comment