简体   繁体   English

在同一活动中使用android.support.v4.app.Fragment和android.app.Fragment

[英]Use android.support.v4.app.Fragment and android.app.Fragment in same activity

My MainActivity uses two types of fragments: 我的MainActivity使用两种类型的片段:

com.google.android.gms.maps.SupportMapFragment com.google.android.gms.maps.SupportMapFragment

and

com.ankitshubham97.myapp.NotifFragment. com.ankitshubham97.myapp.NotifFragment。

For com.google.android.gms.maps.SupportMapFragment , android.support.v4.app.Fragment is needed but for com.ankitshubham97.myapp.NotifFragment , android.app.Fragment is needed. 对于com.google.android.gms.maps.SupportMapFragment ,需要android.support.v4.app.Fragment ,而对于com.ankitshubham97.myapp.NotifFragment ,则需要android.app.Fragment I cannot import both of them. 我不能同时导入它们。 Is there some neat solution for solving this? 是否有解决此问题的灵巧解决方案? I already looked into How to mix android.support.v4.app.Fragment and android.app.Fragment and Android using both getFragmentManager and getSupportFragmentManager causes overlapping but they are a bit old and thought if somebody has a neat solution, apart from what is suggested in the previous links. 我已经研究了如何同时 使用getFragmentManager和getSupportFragmentManager 混合使用android.support.v4.app.Fragment和android.app.FragmentAndroid导致重叠,但是它们有些陈旧,并且考虑到有人可以提供一种整洁的解决方案,除了在前面的链接中建议。

Here is my activity.xml's fragment declaration part: 这是我的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>

My MainActivity.java's onCreate function(with errors): 我的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;
            }
        });
}

Where-ever you use "Fragment", use the specific class including the package. 无论在何处使用“片段”,都应使用特定的类,包括包装。 So replace "Fragment" with "android.support.v4.app.Fragment" or "android.app.Fragment" depending on the case. 因此,根据情况,将“ Fragment”替换为“ android.support.v4.app.Fragment”或“ android.app.Fragment”。

Does that solve your issue? 这样可以解决您的问题吗?

I found out the problem. 我发现了问题。 NotifFragment class extended android.support.v4.app.Fragment and that's why I had to use getSupportFragmentManager().findFragmentById(R.id.notif); NotifFragment类扩展了android.support.v4.app.Fragment ,这就是为什么我不得不使用getSupportFragmentManager().findFragmentById(R.id.notif); instead of getFragmentManager().findFragmentById(R.id.notif); 而不是getFragmentManager().findFragmentById(R.id.notif); . Also in imports, I had to use android.support.v4.app.* instead of android.app.* 同样在导入中,我不得不使用android.support.v4.app.*而不是android.app.*

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在实现FragmentTabHost时将android.support.v4.app.Fragment强制转换为android.app.Fragment的可能性 - Possibility of casting android.support.v4.app.Fragment to android.app.Fragment when implementing a FragmentTabHost 无法从android.support.v4.app.Fragment转换为android.app.Fragment - Cannot convert from android.support.v4.app.Fragment to android.app.Fragment 类型不匹配:无法从android.support.v4.app.Fragment转换为android.app.Fragment - Type mismatch: cannot convert from android.support.v4.app.Fragment to android.app.Fragment 使用backstack时android.app.Fragment和android.support.v4.app.Fragment的不同行为 - Different behavior of android.app.Fragment and android.support.v4.app.Fragment while using backstack 错误:(31,40)错误:不兼容的类型:android.support.v4.app.Fragment无法转换为android.app.Fragment - Error:(31, 40) error: incompatible types: android.support.v4.app.Fragment cannot be converted to android.app.Fragment 必需的android.support.v4.app.fragment - required android.support.v4.app.fragment 有没有一种方法可以将片段转换为android.support.v4.app.Fragment - Is there a way to convert fragment to android.support.v4.app.Fragment 覆盖 android.support.v4.app.fragment 中已弃用的方法 - Override deprecated method in android.support.v4.app.fragment 如何从活动中打开android.support.v4.app.Fragment - how do i open up android.support.v4.app.Fragment from an activity android.support.v4.app.fragment和androidx.fragment.app.FragmentActivity有什么区别 - What is the difference between android.support.v4.app.fragment and androidx.fragment.app.FragmentActivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM