我正试图让我的导航抽屉顺利运行,因为在打开和关闭抽屉时,过渡时会出现断续/延迟。我希望这能完美运行。有什么帮助吗?
import numpy as np
import pandas as pd
np.random.seed(0)
df = pd.DataFrame(np.random.rand(4,5), columns = list('abcab'))
print df
a b c a b
0 0.548814 0.715189 0.602763 0.544883 0.423655
1 0.645894 0.437587 0.891773 0.963663 0.383442
2 0.791725 0.528895 0.568045 0.925597 0.071036
3 0.087129 0.020218 0.832620 0.778157 0.870012
答案 0 :(得分:3)
文档:Click
试试这个:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
new Handler.postDelayed(new Runnable() {
@Override
public void run() {
switch (item.getItemId()) {
case: R.id.example1:
// do something
break;
case: R.id.example2:
// do something
break;
default: // do something
}
}, 200);
drawer.closeDrawer(GravityCompat.START);
return true;
}
在我的拙见中,你需要避免每次点击都叫“新”。
要解决此问题,您可以使用常量值来导航抽屉
android:layout_width
和android:layout_height
属性即
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
您可能还想在AndroidManifest.xml
<activity
android:name=".ui.SomeActivity"
android:hardwareAccelerated="true" />
答案 1 :(得分:1)
滞后是由同一个线程(主线程a.k.a. UI线程)中发生的两个繁重的操作(片段替换和抽屉动画)引起的。
解决此问题的一种方法是在抽屉完全关闭后调用碎片替换。这样两个操作就不会同时发生。
此外,它是一个很好的设计实现,可以使用片段容器淡化进度条。
在Activity
内你应该有这样的东西:
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Show progress bar and hide content container
crossfade(progressBar, container, false)
drawerLayout?.addDrawerListener(object : DrawerLayout.DrawerListener {
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {}
override fun onDrawerOpened(drawerView: View) {}
override fun onDrawerStateChanged(newState: Int) {}
override fun onDrawerClosed(drawerView: View) {
// This method will be called after drawer animation finishes
// Perform the fragment replacement
when (item.itemId) {
R.id.drawer_item_1 -> {
supportFragmentManager.beginTransaction()
.replace(R.id.container, MyFragmentOne.newInstance())
.addToBackStack(null)
.commit()
}
R.id.drawer_item_2 -> {
supportFragmentManager.beginTransaction()
.replace(R.id.container, MyFragmentTwo.newInstance())
.addToBackStack(null)
.commit()
}
}
// Cross fade back the content container and hide progress bar
crossfade(container, progressBar, false)
// Remove this listener so close by, for example, swiping do not call it again
drawerLayout.removeDrawerListener(this)
}
})
// Closes the drawer, triggering the listener above
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
private fun crossfade(viewIn: View, viewOut: View, animateViewOut: Boolean = true) {
val crossfadeDuration = 200L
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
viewIn.alpha = 0f
viewIn.visibility = View.VISIBLE
viewIn.bringToFront()
// Animate the in view to 100% opacity, and clear any animation
// listener set on the view.
viewIn.animate()
.alpha(1f)
.setDuration(crossfadeDuration)
.setListener(null)
// Animate the out view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
viewOut.animate()
.alpha(0f)
.setDuration(if (animateViewOut) crossfadeDuration else 0)
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
viewOut.visibility = GONE
}
})
}
在你的xml中,你应该有这样的东西:
...
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:openDrawer="start">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>
....
答案 2 :(得分:-1)
调用drawerLayout.closeDrawers()后,创建一个新的线程
new Thread(new Runnable() {
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(300);
//Here call you fragmentManager
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
当然,您可以根据需要改变MILISECONDS。对不起我的英文
答案 3 :(得分:-1)
滞后是由于在同一线程(主线程又称为UI线程)中发生两次繁重的操作(片段替换和抽屉动画)引起的。
在我的情况下,导航标题背景图片的尺寸很大,因此它可以在高性能设备上使用,而在低性能设备上则没有。