2

我有一个带有一个活动和很多片段的小应用程序;我与 SherlockFragment 一起工作。我的问题是我不知道如何保存片段的实例以获取出现在特殊导航列表项上的最后一个片段。

例如:我选择一个导航列表项,例如“信息”。然后我得到 FragmentA,FragmentA 从 SherlockListFragment 扩展,所以我可以选择一个 ListItem,然后我得到 FragmentB。FragmentB 也从 SherlockListFragment 扩展,所以我得到 FragmentC 等等。

现在问题如下:当我选择另一个导航列表项目时,例如“home”,我得到 FragmentD。现在我再次选择我的导航列表项“信息”,我希望出现 FragmentC。

在我的代码片段中,我只是替换了片段,但我需要知道哪个片段显示为最后。

我的 SherlockFragmentActivity:

public class MainActivity extends SherlockFragmentActivity implements ActionBar.OnNavigationListener{
    private FragmentHome mFragmentHome = null;
    private FragmentInfo mFragmentInfo = null;


    private static final String mListItemHome = "starthome";
    private static final String mListItemInfo = "startinfo";

public void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.My_Theme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_fragment);

    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    Context context = getSupportActionBar().getThemedContext();
    // Navlist
    ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context,
            R.array.locations, R.layout.sherlock_spinner_dropdown_item);
    list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(list, this);

    mFm = getSupportFragmentManager();
    //Init of my Fragments:              
    mFragmentHome = new FragmentHome();
    mFragmentInfo = new FragmentInfo();
    }

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {

    FragmentTransaction ft = mFm.beginTransaction();
        switch(itemPosition){
        case 0:
            ft.replace(R.id.fragment_content, mFragmentHome);
            ft.addtoBackStack(mListHome);
            ft.commit();
            return true;
        case 1:
            ft.replace(R.id.fragment_content, mFragmentInfo);
            ft.addtoBackStack(mListInfo);
            ft.commit();
            return true;
        default:
            return false;
       }
  }
}

我的片段看起来像这样:

 public class FragmentInfo extends SherlockListFragment{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    //disable the HomeButton as "Up"-Button
    ActionBar actionBar = getSherlockActivity().getSupportActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    ...
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    Bundle bundle = new Bundle();
    bundle.putString("x", x );
    bundle.putString("y", y);

    FragmentManager fm = getFragmentManager();
    FragmentInfo fragment = new FragmentInfo(fm);
    fragment.setArguments(bundle);

    FragmentTransaction ft = fm.beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.replace(R.id.fragment_content, fragment);
    ft.addToBackStack("info");
    ft.commit();        
    }
}

我真的整天都在寻找解决方案,但我没有找到可以帮助我的东西。我想我需要这样的标签: Actionbarsherlock + 标签 + 多片段? 或者类似 TabHost 的列表...

我希望你能帮助我。

最诚挚的问候

编辑:

我只是遇到了隐藏和显示的问题。在我的 xml 布局中,我有一个带有 id 的 Framelayout:fragment_content。此 id 始终用于将一个片段替换为另一个片段。现在我添加另一个带有 id 的 Framelayout:fragment_info。我这样做是为了得到我用 FragmentManager 替换的最后一个 Fragment。

现在,我的 NavigationItemSelected 方法看起来像这样:

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    FragmentTransaction ft = mFm.beginTransaction();
    switch(itemPosition){
    case 0:
       if(mFragmentHome==null){
           mFragmentHome = new FragmentHome();
           ft.replace(R.id.fragment_content, mFragmentHome)
           ft.addToBackStack(mListItemHome);
           ft.commit();
       }else{
           if(mFragmentHome.isHidden()){
               ft.show(mFragmentHome);
               ft.replace(R.id.fragment_content, mFragmentHome)
               ft.addToBackStack(mListItemHome);
               ft.commit();
           }
           ft.hide(mFragmentInfo);
       }
    case 1:
       if(mFragmentInfo==null){
           mFragmentInfo = new FragmentInfo();
           ft.replace(R.id.fragment_info, mFragmentInfo)
           ft.addToBackStack(mListItemInfo);
           ft.commit();
           ft.hide(mFragmentHome)
       }else{
           ft.hide(mfragmentHome();
           //here I get the last Fragment I choose under the navlist-Item "Info"
           Fragment f = getFragmentManager.findFragmentById(R.id.fragment_info);
           ft.replace(R.id.fragment_info, f);
           ft.addToBackStack(mListItemInfo);
           ft.commit();
           }
           ft.hide(mFragmentHome);
   default:
   return false;
   }
}
}

我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <FrameLayout
        android:id="@+id/fragment_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
        <FrameLayout
        android:id="@+id/fragment_info"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>    

但是现在我的后退按钮有另一个问题。当我选择菜单信息并浏览 FragmentA 到 FragmentB 到 FragmentC 时,然后我选择 navlist-Item "Home" 并再次选择 navlist-Item "Info",所以我得到了 FragmentC。但是当我现在按下 BackButton 时,Activity 将被销毁。

但我希望在按下 BackButton 后出现 FragmentB -> FragmentA,现在我可以完成 Activity。

我知道我可以覆盖后退按钮,但我想我犯了一些错误。也许我应该使用分离和附加?我认为应该有一个更简单的方法,不是吗?

4

1 回答 1

0

我知道为时已晚。但它会帮助其他一些人。使用显示和隐藏而不是替换。这是一个示例代码。

private class MyTabListener implements ActionBar.TabListener {
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        switch (tab.getPosition()) {
        case 0:

            if (frag1 == null) {
                // If not, instantiate and add it to the activity
                frag1 = Fragment.instantiate(getApplicationContext(),
                        FeedsActivity.class.getName());
                ft.add(android.R.id.content, frag1, "Feeds");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag1);
            }
            return;

        case 1:
            if (frag2 == null) {
                // If not, instantiate and add it to the activity
                frag2 = Fragment.instantiate(getApplicationContext(),
                        ProfileActivity.class.getName());
                ft.add(android.R.id.content, frag2, "Profile");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag2);
            }
            return;
        case 2:

            if (frag3 == null) {
                // If not, instantiate and add it to the activity
                frag3 = Fragment.instantiate(getApplicationContext(),
                        History.class.getName());
                ft.add(android.R.id.content, frag3, "History");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag3);
            }

            return;

        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        if (frag1 != null) {
            // Detach the fragment, because another one is being attached
            switch (tab.getPosition()) {
            case 0:
                ft.hide(frag1);
                return;
            case 1:
                ft.hide(frag2);
                return;
            case 2:
                ft.hide(frag3);
                return;

            }

        }
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
}
于 2013-04-18T07:34:49.057 回答