[英]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.Fragment和Android导致重叠,但是它们有些陈旧,并且考虑到有人可以提供一种整洁的解决方案,除了在前面的链接中建议。
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.