【问题标题】:Use android.support.v4.app.Fragment and android.app.Fragment in same activity在同一个活动中使用 android.support.v4.app.Fragment 和 android.app.Fragment
【发布时间】:2017-09-04 08:21:10
【问题描述】:

我的 MainActivity 使用了两种类型的片段:

com.google.android.gms.maps.SupportMapFragment

com.ankitshubham97.myapp.NotifFragment.

对于com.google.android.gms.maps.SupportMapFragment,需要android.support.v4.app.Fragment,但对于com.ankitshubham97.myapp.NotifFragment,需要android.app.Fragment。我不能同时导入它们。有没有一些巧妙的解决方案来解决这个问题?我已经研究过How to mix android.support.v4.app.Fragment and android.app.FragmentAndroid using both getFragmentManager and getSupportFragmentManager causes overlapping,但它们有点老了,我想如果有人有一个简洁的解决方案,除了前面链接中的建议。

这是我的activity.xml的片段声明部分:

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </fragment>

    <fragment
        android:id="@+id/notif"
        android:name="com.ankitshubham97.myapp.NotifFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </fragment>
</FrameLayout>

我的 MainActivity.java 的 onCreate 函数(有错误):

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        notifFragment =  getFragmentManager().findFragmentById(R.id.notif);
        getFragmentManager().beginTransaction().hide(notifFragment);
        mapFragment.getMapAsync(this);
        navigation = (AHBottomNavigation) findViewById(R.id.navigation);
        AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.title_home, R.drawable.ic_home_black_24dp, R.color.colorBottomNavigationPrimary);
        AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.title_dashboard, R.drawable.ic_dashboard_black_24dp, R.color.colorBottomNavigationPrimary);
        AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.title_notifications, R.drawable.ic_notifications_black_24dp, R.color.colorBottomNavigationPrimary);
        navigation.addItem(item1);
        navigation.addItem(item2);
        navigation.addItem(item3);
        navigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
            @Override
            public boolean onTabSelected(int position, boolean wasSelected) {
                FragmentManager sfm = getSupportFragmentManager();
                FragmentTransaction sft= sfm.beginTransaction();
                switch (position) {
                    case 0:
                        Log.d(TAG,(mapFragment==null)+"");
                        sft.show(mapFragment);
                        ft.hide(notifFragment);
                        sft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                        sft.commit();
                        Toast.makeText(getApplicationContext(),R.string.title_home,Toast.LENGTH_SHORT).show();
                        return true;

                    case 1:

                        sft.hide(mapFragment);
                        ft.show(notifFragment);
                        sft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                        sft.commit();addToBackStack(null).commit();
                        Toast.makeText(getApplicationContext(),R.string.title_dashboard,Toast.LENGTH_SHORT).show();
                        return true;
                    case 2:
                        Toast.makeText(getApplicationContext(),R.string.title_notifications,Toast.LENGTH_SHORT).show();
                        navigation.setNotification("",2);
                        spEditor.putInt(NOTIFCOUNT,0);
                        spEditor.apply();
                        return true;
                }
                return true;
            }
        });
}

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    无论您在何处使用“片段”,都应使用包括包在内的特定类。因此,视情况将“Fragment”替换为“android.support.v4.app.Fragment”或“android.app.Fragment”。

    这能解决您的问题吗?

    【讨论】:

    • 感谢您的回复。实际上问题是别的(见我的回答),我已经事先尝试过你的方法。 :)
    【解决方案2】:

    我发现了问题。 NotifFragment 类扩展 android.support.v4.app.Fragment 这就是为什么我必须使用 getSupportFragmentManager().findFragmentById(R.id.notif); 而不是 getFragmentManager().findFragmentById(R.id.notif);。同样在导入时,我不得不使用android.support.v4.app.* 而不是android.app.*

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多