将DrawerLayout
与NavigationView
和FrameLayout
一起使用我想切换Fragment
。这很好用。
但是,如果我切换得太快,那么Fragment
s会重叠......
就像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(太快)(手动或通过我的Nexus 5延迟750毫秒的代码),我会使两个Fragment
重叠,第二个{ {1}}启用了触摸功能但第一个Fragment
位于顶部...
第一个Fragment
包含Fragment
和ImageView
个。第二个TextView
包含Fragment
和TabLayout
(如果我的问题可以解决这个问题)。是的我正在使用AppCompat 22.2.0和Design 22.2.0库。
如果我为两者设置背景颜色,那么我只能看到第一个ViewPager
,而且它永远不会改变。
我尝试了Fragment
,popBackStackImmediate()
,executePendingTransactions()
,remove(fragment)
,Android: fragments overlapping issue,延迟等等,但没有成功。
android:fitsSystemWindows="true"
修改
在我的@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());
}
}
没有阻止这种奇怪的问题......
编辑2
我考虑过保持executePendingTransactions()
(init为false)来跟踪boolean
事务发生的时间。我将Fragment
中的true
和navigationDrawer()
中的false
设置为Fragment.onResume()
。仍然没有去......
所以:MainFragment
使用Picasso
加载图片并将切换过快(800ms)到另一个Fragment
仍有问题:它们仍然重叠......
答案 0 :(得分:1)
你可以尝试这个...... 它会延迟你的频繁点击......
private final Handler mDrawerActionHandler = new Handler();
private static final long DRAWER_CLOSE_DELAY_MS = 250;
mDrawerActionHandler.postDelayed(new Runnable() {
@Override
public void run() {
// your navigation code goes here
}
}, DRAWER_CLOSE_DELAY_MS);
答案 1 :(得分:0)
在我的onCreate()
我这样做:
if (savedInstanceState == null) {
final MenuItem menuItem = mNavigationView.getMenu().getItem(0);
if (menuItem != null) {
navigationDrawer(menuItem.getItemId());
}
}
结果是打电话太快了。 删除此代码解决了我的问题。
我仍然不知道:
为什么
executePendingTransactions()
没有阻止这种奇怪的问题...
修改强>
我的MainFragment
使用Picasso
加载图片并将切换速度过快(800毫秒)到另一张图片时仍有问题:它们仍然重叠......
编辑2
所以我现在使用布尔值来标记我的Activity
是否正在转换Fragment
,并且交换机之间的最小定时器为1秒。
在我的Fragment
onViewCreated()
:
if (view != null) {
view.post(new Runnable() {
@Override
public void run() {
// Activity might be null
getActivity().setTransitioning(false);
}
});
}
在我的navigationDrawer()
:
if (isTransitioning()) {
// Switching Fragments too fast
return false;
}