java - How to display fragment in container when selected from an action bar? -
my app right consists of google maps implementation action bar spinner. how make fragment instantiation when select item on navigation bar? put, i'm trying display screen when click on 1 of selections. sort of have onnavigationitemselected() function implemented don't know take here. here's code.
package com.example.andrew.ubair4; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.support.v4.app.fragmentactivity; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.content.context; import android.os.build; import android.os.bundle; import android.widget.toast; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.onmapreadycallback; public class ubair extends actionbaractivity implements actionbar.onnavigationlistener, onmapreadycallback{ /** * serialization (saved instance state) bundle key representing * current dropdown position. */ private static final string state_selected_navigation_item = "selected_navigation_item"; private googlemap googlemap; // might null if google play services apk not available. @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_ubair); // set action bar show dropdown list. final actionbar actionbar = getsupportactionbar(); actionbar.setdisplayshowtitleenabled(false); actionbar.setnavigationmode(actionbar.navigation_mode_list); // set dropdown list navigation in action bar. actionbar.setlistnavigationcallbacks( // specify spinneradapter populate dropdown list. new arrayadapter<string>( actionbar.getthemedcontext(), android.r.layout.simple_list_item_1, android.r.id.text1, new string[]{ getstring(r.string.title_section1), getstring(r.string.title_section2), getstring(r.string.title_section3), }), this); driverorderslist.newinstance("hi","hi"); supportmapfragment nmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)); googlemap = nmap.getmap(); googlemap.setmylocationenabled(true); } @override public void onrestoreinstancestate(bundle savedinstancestate) { // restore serialized current dropdown position. if (savedinstancestate.containskey(state_selected_navigation_item)) { getsupportactionbar().setselectednavigationitem( savedinstancestate.getint(state_selected_navigation_item)); } } @override public void onsaveinstancestate(bundle outstate) { // serialize current dropdown position. outstate.putint(state_selected_navigation_item, getsupportactionbar().getselectednavigationindex()); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_ubair, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public boolean onnavigationitemselected(int position, long id) { // when given dropdown item selected, show contents in // container view. getsupportfragmentmanager().begintransaction() .replace(r.id.container, placeholderfragment.newinstance(position + 1)) .commit(); return true; } @override public void onmapready(googlemap map) { latlng sydney = new latlng(-33.867, 151.206); map.setmylocationenabled(true); map.movecamera(cameraupdatefactory.newlatlngzoom(sydney, 13)); map.addmarker(new markeroptions() .title("sydney") .snippet("the populous city in australia.") .position(sydney)); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; /** * returns new instance of fragment given section * number. */ public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public placeholderfragment() { } /* @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_ubair, container, false); return rootview; } */ } }
here's xml
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ubair" tools:ignore="mergerootframe" > <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.google.android.gms.maps.supportmapfragment" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> </framelayout>
hi in xml build fragments way:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="mergerootframe" />
and in activity:
phf = placeholderfragment.newinstance(); if (savedinstancestate == null) { getfragmentmanager().begintransaction().add(r.id.container, phf).commit(); }
but in case want replace them, in spinner listener can do:
phf = accountfragment.newinstance(position, this); fragmentmanager.begintransaction().replace(r.id.container, accountfragment).commit();
i edit in response of commnent:
@override public boolean onnavigationitemselected(int position, long id) { switch(position) { case 0: fragment0 = fragment0.newinstance(); fragmentmanager.begintransaction().replace(r.id.container, fragment0).commit(); break; case 1: fragment1 = fragment1.newinstance(); fragmentmanager.begintransaction().replace(r.id.container, fragment1).commit(); break; //etc... } return true; }
or can declare fragments activity's attributes , replace them in callback.
Comments
Post a Comment