gpt4 book ai didi

android - 遍历Fragment返回栈时如何刷新ActionBar导航项?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:05:49 25 4
gpt4 key购买 nike

Android 4.1 ActionBar 提供了一种有用的导航模式,如列表或选项卡。我正在使用 SpinnerAdapter 从三个要显示在 View android.R.id.content 中的 fragment 中进行选择。onNavigationItemSelected() 监听器然后将每个 fragment 膨胀到 View ,并使用 FragmentTransaction.addToBackStack(null) 将其添加到返回堆栈。

这一切都像宣传的那样工作,但我不知道如何更新 ActionBar 以反射(reflect)当前的返回堆栈。使用 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;
}
}

最佳答案

你可以这样做:

  1. 当您选择基于后退按钮的导航项时,创建一个 bool 值来跟踪:

    private boolean mListIsNavigatingBack = false;
  2. 重写 onBackPressed,在重写中,检查 backstack 是否有项目,如果有请自行处理,如果没有则调用父类(super class):

    public void onBackPressed() {
    if(getFragmentManager().getBackStackEntryCount() > 0){
    mListIsNavigatingBack = true;

    //You need to get the previous index in the backstack through some means
    //possibly by storing it in a stack
    int previousNavigationItem = ???;

    getActionBar().setSelectedNavigationItem(previousNavigationItem);
    }
    else{
    super.onBackPressed();
    }
    }
  3. 在 NavigationListener 内部,处理 mListIsNavigatingBack 状态,手动弹出返回堆栈并取消设置状态:

    if(mListIsNavigatingBack){
    if(fm.getBackStackEntryCount() > 0){
    fm.popBackStack();
    }
    mListIsNavigatingBack = false;
    }

关于android - 遍历Fragment返回栈时如何刷新ActionBar导航项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12943194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com