【问题标题】:Add Fragment on FrameLayout in Android在 Android 的 FrameLayout 上添加 Fragment
【发布时间】:2015-12-17 22:28:56
【问题描述】:

我的 Activity 已扩展到 AppCompatActivity。我也成功设置了一个抽屉。现在,当我在 Fragment 上进行交易时,它基本上使用 begintransaction。但我在android.app.FragmentManager fragmentManager = getFragmentManager(); 上收到此错误消息Unreachable statement,我还尝试了 android.support.v4.app 包它也说这是不推荐使用的。

在我的抽屉导航onClickListener

我有这个

`

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                android.app.Fragment objFragment = null;

                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {
                    case R.id.dhome:
                        objFragment = new panel();
                        mCurrentSelectedPosition = 0;
                        return true;
                    case R.id.dsetting:

                        mCurrentSelectedPosition = 1;
                        return true;
                    case R.id.dlogout:

                        mCurrentSelectedPosition = 2;
                        return true;
                    default:
                        return true;
                }

                // update the main content by replacing fragments
                if(objFragment != null) {
                    android.app.FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.container, objFragment)
                            .commit();
                }

            }
        });`

在我的布局中

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
   >


    <FrameLayout android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#fff"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#d9d8d8"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer" />



</android.support.v4.widget.DrawerLayout>

【问题讨论】:

    标签: java android switch-statement fragment


    【解决方案1】:

    使用这个:

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.dhome:
                objFragment = new panel();
                mCurrentSelectedPosition = 0;
                break;
            case R.id.dsetting:
                mCurrentSelectedPosition = 1;
                break;
            case R.id.dlogout:
                mCurrentSelectedPosition = 2;
                break;
        }
    
        // update the main content by replacing fragments
        if(objFragment != null) {
            android.app.FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                           .replace(R.id.container, objFragment)
                           .commit();
        }
    
        return true;
    }
    

    您之前的代码没有考虑用户如何与actionbar 交互(点击任何项目),return true; 将始终在到达片段事务代码之前执行。

    【讨论】:

    • 你的回答是对的 :) 稍后我会接受。无论如何快速的问题..在我的家庭活动中扩展到片段是否可以使用功能齐全的谷歌地图返回com.google.android.gms.maps.SupportMapFragment的布局?
    • 我认为你需要做一些配置。我真的不记得如何使用 Google Maps API 设置地图了。
    • 最终我已经有了。但它不是片段类型。它的 AppCompatActivity。但是可以在 Fragment 上设置谷歌地图吗?
    • 是的,我以前确实见过。
    • 谢谢伙计;)我明白了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多