android - How to instantiate a fragment in a PagerAdapter? -
i have following custom pager adapter, i'm calling activity , passing parameters. working fine. strings in latitude , longitude.
class custompageradapter extends pageradapter { context context; private final arraylist<string> latitude; private final arraylist<string> longitude; public custompageradapter(activity context,arraylist<string> latitude,arraylist<string> longitude) { this.context = context; this.latitude= latitude; this.longitude = longitude; } @override public int getcount() { return latitude.size(); } @override public boolean isviewfromobject(view view, object object) { return view == (object); } @override public object instantiateitem(viewgroup container, int position) { layoutinflater layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view itemview = layoutinflater.inflate(r.layout.pager_item, container, false); imageview imageview = (imageview) itemview.findviewbyid(r.id.listpager_imageview); textview latitude = (textview) itemview.findviewbyid(r.id.listpager_text); textview longitude = (textview) itemview.findviewbyid(r.id.listpager_title); latitude.settext(this.latitude.get(position)); longitude.settext(this.longitude.get(position)); container.addview(itemview); return itemview; } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((relativelayout) object); } } i have map fragment, working fine if i'm calling activity this:
fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); // replace contents of container new fragment ft.replace(r.id.your_placeholder, new mapfragment()); // complete changes added above ft.commit(); this fragment:
public class mapfragment extends fragment { mapview mapview; googlemap map; private double latitude,longitude; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.listviewtopager, container, false); // gets mapview xml layout , creates mapview = (mapview) v.findviewbyid(r.id.mapview); mapview.oncreate(savedinstancestate); // gets googlemap mapview , initialization stuff map = mapview.getmap(); map.getuisettings().setmylocationbuttonenabled(false); map.setmylocationenabled(true); // needs call mapsinitializer before doing cameraupdatefactory calls mapsinitializer.initialize(this.getactivity()); // updates location , zoom of mapview cameraupdate cameraupdate = cameraupdatefactory.newlatlngzoom(new latlng(1.56, 43.433), 10); map.animatecamera(cameraupdate); return v; } @override public void onresume() { mapview.onresume(); super.onresume(); } @override public void ondestroy() { super.ondestroy(); mapview.ondestroy(); } @override public void onlowmemory() { super.onlowmemory(); mapview.onlowmemory(); } } what introduce fragment in custompageradapter, when i'll call custompageradapter activity everytime swipe, new fragment loaded. your_placeholder id
<framelayout android:id="@+id/map_fragment" android:layout_width="match_parent" android:layout_height="match_parent"/> is possible ? i'm sorry if haven't explained problem properly. i'll try edit if have questions. thank you.
edit: question have-> when press button pageractivity starts again, doesn't close it. have press twice close it.
it sure possible replace fragments pageradapter; use it. since brought up, fragmentpageradapter class may make processing fragments easier, hence namesake. have not used though. either way code should similar.
about pressing button. if want have effect. here sample code yours:
fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); // replace contents of container new fragment ft.replace(r.id.your_placeholder, new mapfragment()); ft.addtobackstack(null); // support key // complete changes added above ft.commit(); note: add method addtobackstack
Comments
Post a Comment