एंड्रॉइड 4.1 ActionBar
एक सूची या टैब के रूप में एक उपयोगी नेविगेशन मोड प्रदान करता है। मैं android.R.id.content
देखने में प्रदर्शित होने के लिए तीन टुकड़ों से चयन करने के लिए SpinnerAdapter
का उपयोग कर रहा हूं। onNavigationItemSelected()
श्रोता तब प्रत्येक खंड को दृश्य में बढ़ाता है और इसे FragmentTransaction.addToBackStack(null)
का उपयोग करके बैक स्टैक में जोड़ता है।फ्रैगमेंट बैक स्टैक को घुमाने के दौरान एक्शनबार नेविगेशन आइटम को रीफ्रेश कैसे करें?
विज्ञापित के रूप में यह सब काम करता है, लेकिन मैं ActionBar
अपडेट करने का तरीका ढेर वर्तमान वापस प्रतिबिंबित करने के लिए पता नहीं है। ActionBar.setSelectedNavigationItem(position)
काम करता है लेकिन एक नया OnNavigationListener()
भी ट्रिगर करता है जो एक और FragmentTransaction
बनाता है (प्रभाव जो मैं चाहता हूं)। स्पष्टीकरण के लिए कोड नीचे दिखाया गया है। समाधान के साथ किसी भी मदद की सराहना की है।
public class CalcActivity extends Activity {
private String[] mTag = {"calc", "timer", "usage"};
private ActionBar actionBar;
/** An array of strings to populate dropdown list */
String[] actions = new String[] {
"Calculator",
"Timer",
"Usage"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// may not have room for Title in actionbar
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter<String>(
actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
actions),
// Provide a listener to be called when an item is selected.
new NavigationListener()
);
}
public class NavigationListener implements ActionBar.OnNavigationListener {
private Fragment mFragment;
private int firstTime = 0;
public boolean onNavigationItemSelected(int itemPos, long itemId) {
mFragment = getFragmentManager().findFragmentByTag(mTag[itemPos]);
if (mFragment == null) {
switch (itemPos) {
case 0:
mFragment = new CalcFragment();
break;
case 1:
mFragment = new TimerFragment();
break;
case 2:
mFragment = new UsageFragment();
break;
default:
return false;
}
mFragment.setRetainInstance(true);
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (firstTime == 0) {
firstTime++;
ft.add(android.R.id.content, mFragment, mTag[itemPos]);
} else {
ft.replace(android.R.id.content, mFragment, mTag[itemPos]);
ft.addToBackStack(null);
}
ft.commit();
Toast.makeText(getBaseContext(), "You selected : " +
actions[itemPos], Toast.LENGTH_SHORT).show();
return true;
}
}
public static class CalcFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_calc, container, false);
return v;
}
}
public static class TimerFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_timer, container, false);
return v;
}
}
public static class UsageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_usage, container, false);
return v;
}
}
अपने सुझाव जो मैं एक ढेर के साथ लागू करने के लिए धन्यवाद। अतिरिक्त जटिलता और अद्वितीय यूएक्स को देखते हुए, मुझे इस डिजाइन पैटर्न के बारे में दूसरे विचार हैं। क्या एक्शनबार नेविगेशन का उपयोग करते समय कभी भी ToBackStack() को बेहतर नहीं करना बेहतर है? – GregM
सूची नेविगेशन इसे जटिल बनाता है। आम तौर पर बैक स्टैक का उपयोग आपके वर्तमान दृश्य में टुकड़ों को स्वैप करने के लिए अधिक होता है, इसलिए शायद आपके उपयोग के मामले को पृष्ठभूमि में मौजूद टुकड़े को फिर से जोड़कर सरलीकृत किया जा सकता है। –