我的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.Fragment和Android 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;
}
});
}
答案 0 :(得分:0)
无论您使用“Fragment”,请使用包含该包的特定类。因此,根据具体情况,将“Fragment”替换为“android.support.v4.app.Fragment”或“android.app.Fragment”。
这会解决您的问题吗?
答案 1 :(得分:0)
我发现了问题。 NotifFragment
课程已延长android.support.v4.app.Fragment
,这就是我必须使用getSupportFragmentManager().findFragmentById(R.id.notif);
代替getFragmentManager().findFragmentById(R.id.notif);
的原因。同样在导入中,我必须使用android.support.v4.app.*
而不是android.app.*