使用DrawerLayout
与NavigationView
和FrameLayout
我想切换Fragment
s。这很好。 但是,如果我切换太快,那么Fragment
s重叠...与DrawerLayout/NavigationView重叠的片段
这就像executePendingTransactions()
不起作用。
<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>
如果我切换Fragment
S(太)快速(手动地或通过与750ms之间的延迟上我的Nexus 5代码),I得到两Fragment
s至重叠,与具有使能触摸BUT第二Fragment
第一Fragment
是在上面...
第一Fragment
包含ImageView
和TextView
秒。第二个Fragment
包含TabLayout
和ViewPager
(如果这可能对我的问题有任何的一天)。是的,我正在使用AppCompat 22.2.0和Design 22.2.0库。
如果我将背景颜色设置为两种颜色,那么我只能看到第一个Fragment
,它永远不会改变。
我试过popBackStackImmediate()
,executePendingTransactions()
,remove(fragment)
,android:fitsSystemWindows="true"
,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()
并没有阻止这样奇怪的问题......
EDIT 2
我想过保持boolean
(INIT为false)跟踪时, Fragment
交易发生。我在navigationDrawer()
中将其设置为true
,在Fragment.onResume()
中将其设置为false
。仍然没有去...
所以:我MainFragment
加载使用Picasso
和切换太快(800毫秒)到另一个Fragment
的图像仍然有问题:他们仍然重叠......
我可以看到你更多的代码..所以,我可以帮助你.. – Moinkhan
@Moinkhan哪一部分它?我相信我发布了相关部分。这是一个基本的DrawerLayout/NavigationView实现。 – shkschneider
你提供的代码是完美的。这就是为什么我认为问题可以在代码中,这是不是在这里提供... – Moinkhan