使用带有NavigationView和FrameLayout的DrawerLayout来切换Fragment,效果非常好。但是,如果我切换得太快,那么Fragment将会重叠...
这就像executePendingTransactions()不起作用一样。
如果我(手动或通过代码在我的Nexus 5上延迟750毫秒)快速切换片段,我会使两个片段重叠,第二个片段具有触摸功能,但第一个片段位于顶部... 第一个片段包含ImageView和TextView。第二个片段包含TabLayout和ViewPager(如果这可能与我的问题有关)。是的,我正在使用AppCompat 22.2.0和Design 22.2.0库。如果我为两者设置背景颜色,那么我只能看到第一个片段,并且它永远不会更改。我尝试了popBackStackImmediate(),executePendingTransactions(),remove(fragment),android:fitsSystemWindows =“true”,{{link1:Android:fragments overlapping issue}},延迟以及其他一些事情,但没有成功。
<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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="@240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/transparent"
android:dividerHeight="0dp"
app:menu="@menu/navigationdrawer" />
</android.support.v4.widget.DrawerLayout>
如果我(手动或通过代码在我的Nexus 5上延迟750毫秒)快速切换片段,我会使两个片段重叠,第二个片段具有触摸功能,但第一个片段位于顶部... 第一个片段包含ImageView和TextView。第二个片段包含TabLayout和ViewPager(如果这可能与我的问题有关)。是的,我正在使用AppCompat 22.2.0和Design 22.2.0库。如果我为两者设置背景颜色,那么我只能看到第一个片段,并且它永远不会更改。我尝试了popBackStackImmediate(),executePendingTransactions(),remove(fragment),android:fitsSystemWindows =“true”,{{link1:Android:fragments overlapping issue}},延迟以及其他一些事情,但没有成功。
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
navigationDrawer(menuItem.getItemId());
return true;
}
});
if (savedInstanceState == null) {
final MenuItem menuItem = mNavigationView.getMenu().getItem(0);
if (menuItem != null) {
navigationDrawer(menuItem.getItemId());
}
}
}
private void navigationDrawer(final int itemId) {
final Fragment fragment = getFragment(itemId);
getSupportFragmentManager().beginTrasaction()
.replace(R.id.frameLayout, fragment)
.addToBackStack(null)
.commit();
mNavigationView.getMenu().findItem(itemId).setChecked(true);
mDrawerLayout.closeDrawer(mNavigationView);
supportInvalidateOptionsMenu();
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_first:
case R.id.menu_second:
case R.id.menu_third:
navigationDrawer(item.getItemId());
return true;
}
return super.onOptionsItemSelected(item);
}
编辑
在我的onCreate()
中,我正在执行以下操作:
if (savedInstanceState == null) {
final MenuItem menuItem = mNavigationView.getMenu().getItem(0);
if (menuItem != null) {
navigationDrawer(menuItem.getItemId());
}
}
这导致调用过快。删除此代码解决了我的问题(暂时性解决,见下文)。
我仍然不知道为什么executePendingTransactions()
没有防止这种奇怪的问题...
编辑2
我考虑保留一个布尔值(初始化为false)来跟踪Fragment
事务发生的时间。我在navigationDrawer()
中将其设置为true
,在Fragment.onResume()
中将其设置为false
。还是不行...
所以:仍然存在问题,即我的MainFragment
使用Picasso
加载图像,并在800ms内太快地切换到另一个Fragment
:它们仍然重叠...
onCreate()
函数。 - shkschneider