【问题标题】:Call other Fragment from dropDown of ActionBar从 ActionBar 的下拉菜单中调用其他 Fragment
【发布时间】:2014-09-26 13:24:49
【问题描述】:

在我的应用程序中,我在 ActionBar 中添加了一个 DropDown Spinner,但现在无法确定 - 如何更改每个选定下拉菜单的底部。在我的 activity_main.xml 布局(主页)中,仅包含一个显示列表的片段。我正在寻找的是,将其他 UI 屏幕也创建为 Fragment,并在每个选定的 dropDown 上更改放置的 Fragment。我的意思是主页有fragment_main.xml,选择下拉设置然后显示setting_fragment.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=".MainActivity"
tools:ignore="MergeRootFrame" >

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="terryconsulting.servicestation.AppointListFragment"
    android:id="@+id/main_fragment"
    />
</FrameLayout>

在 MainActivity 类中,我实现了 ActionBar.OnNavigationListener,它调用了之前在屏幕上显示 Hello World 文本的 PlaceholderFragment。

@Override
public boolean onNavigationItemSelected(int position, long id) {
    // When the given dropdown item is selected, show its contents in the
    // container view.
    //getFragmentManager().beginTransaction()
    //        .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
    //        .commit();
    return true;
    // http://www.vogella.com/tutorials/AndroidActionBar/article.html --- ACTIONBAR ACTION
}

// As AppointListFragment is added in this activity, so need to implement this listener here
@Override
public void onFragmentInteraction(Uri uri) {
    System.out.println("Into onFragmentInteraction. URL - " + uri);

    return;
}

我卡在哪里:- 从下拉菜单中选择设置时,我无法确定如何调用/设置 SettingFragment。并且从设置按下Home图标返回到带有AppointListFragment的主页???

我的想法和要求,是否可行和实用?我认为应该是,因为将 Setting 称为具有自己的 ActionBar 的 Activity 并不是最好的解决方案。

请指导我,我已经搜索了很多教程,但在任何地方都找不到这种情况或带有 NavigationSelection 和 Fragments 的示例。

【问题讨论】:

    标签: android android-fragments drop-down-menu navigation android-actionbar


    【解决方案1】:

    XML 布局中的片段是静态的。我很可能以某种方式使它们工作,但对于动态生成/创建/删除,你想用 Java 做所有事情。

    说,首先你从你的 XML 中删除 &lt;fragment,然后在你的活动创建上:

    @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(... 你的 xml 布局 ... );

       // fragments are auto-re-created on rotation,
       // so only create/put in layout one if not rotating
       if(savedInstanceState == null){
    
           // I'm assuming here your first fragment is position 0
           gotoFragment(0);
       }
    

    }

    然后在每个选择上只是调用新的:

    @Override
    public boolean onNavigationItemSelected(int position, long id) {
        gotoFragment(position);
        return true;
    }
    

    这是更改屏幕上的片段的一般方法。

    private void gotoFragment(int position){
           Fragment f = getFragmentManager().findFragmentByTag("position_" + position);
           if(f == null){
               switch(position){
                   case values:
                      f = // create here new object of your fragment for each position
               }
           }
    
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, f, "position_" + position)
                    .commit();
    }
    

    【讨论】:

    • 谢谢@Budius。上面的代码对我帮助很大。但是我面临 onCreate 代码的问题。由于我在 ActionBar 中启用了 Home,并且还在添加要在主页上添加的片段 (position_0),因此我没有在我的下拉菜单中添加 Home。导航每次默认在“onCreate”中选择第一个元素,所以一开始我看到我的第 0 个片段(位置_0)和立即第一个片段(位置_1)。我试过'actionBar.setSelectedNavigationItem(-1);',但它没有区别。我不想在下拉菜单中添加主页,因为主页图标点击已经是他们的了。知道如何克服这个问题!
    • 当 Home 被选中时,下拉选择的项目上不应该看到任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    相关资源
    最近更新 更多