我在一个活动的两个片段。当碎片A显示时,我想的抽屉式导航汉堡图标显示和导航抽屉工作。当B片段显示时,我想后退箭头显示,当它被点击做一个向上导航。不过,我似乎无法得到新的AppCompat V7工具栏,显示向上箭头我所有的ActionBarActivity内,除非资产净值抽屉是打开的。
I have two fragments in an activity. When fragment A is showing, I want the navigation drawer burger icon to show and the navigation drawer to work. When fragment B is showing, I want the back arrow to show and when it's clicked do an up navigation. However, I can't seem to get the new AppCompat v7 toolbar to show the up arrow at all inside my ActionBarActivity unless the nav drawer is open.
在我的活动,我的onCreate()方法,我有...
In my activity, for my onCreate() method I have...
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
然后我叫 mDrawerToggle.syncState();
在我onPostCreate()
And then I call mDrawerToggle.syncState();
in my onPostCreate()
我试图寻找关于如何编程触发工具栏图标返回箭头,但没有奏效。从我收集的,叫
I've tried searching on how to programatically trigger the toolbar icon to the back arrow but nothing has worked. From what I've gathered, calling
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
这是我的片断应该更改图标,但事实并非如此。这可能是一个愚蠢的问题,但我究竟做错了什么?
from my fragment should change the icon but that's not the case. This may be a stupid question, but what am I doing wrong?
这是我所看到的在第7版 ActionBarDrawerToggle
来源$ C $ C,可以动画图标,不同的状态,而不必被拉开抽屉。
From what I have seen in the source code of v7 ActionBarDrawerToggle
, you can animate the icon to different states without having the drawer being opened.
private enum ActionDrawableState{
BURGER, ARROW
}
private static void toggleActionBarIcon(ActionDrawableState state, final ActionBarDrawerToggle toggle, boolean animate){
if(animate) {
float start = state == ActionDrawableState.BURGER ? 0f : 1.0f;
float end = Math.abs(start - 1);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ValueAnimator offsetAnimator = ValueAnimator.ofFloat(start, end);
offsetAnimator.setDuration(300);
offsetAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
offsetAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float offset = (Float) animation.getAnimatedValue();
toggle.onDrawerSlide(null, offset);
}
});
offsetAnimator.start();
}else{
//do the same with nine-old-androids lib :)
}
}else{
if(state == ActionDrawableState.BURGER){
toggle.onDrawerClosed(null);
}else{
toggle.onDrawerOpened(null);
}
}
}
汉堡和箭头之间的变形取决于之间的值0F
和 1.0F
,基本上这些都是值抽屉传递给ActionBarDrawerToggle
Morphing between Burger and Arrow depends on values between 0f
and 1.0f
, basically these are values that the drawer passes to the ActionBarDrawerToggle.
我用 ValueAnimator
在此范围内,即模仿抽屉反复动画的值。
I used ValueAnimator
to animate values in this range, i.e mimicking the drawer toggling.
空
参数是安全的,因为 ActionBarDrawerToggle
不关心的所有关于抽屉的看法。
确保你看看新的插补器完全由这本书的材料设计方针做动画:
null
arguments are safe because ActionBarDrawerToggle
does not care at all about drawer views.
Make sure you take a look at new interpolators to do the animation fully-by-the-book of material design guidelines:
fast_out_linear_in
fast_out_slow_in
另一种方法是访问 mSlider
的 ActionBarDrawer
的私有字段通过反射和调用 setPosition两种(浮动位置)
方法,汉堡和箭头之间切换。
mSlider
的类型是(扩展) DrawerArrowDrawable
。
Another approach is to access mSlider
private field of the ActionBarDrawer
through reflection and call setPosition(float position)
method to toggle between Burger and Arrow.
mSlider
is of type (extends) DrawerArrowDrawable
.
就个人而言,我总是尽量避免反射,只要没有其他的方式做你肮脏的工作。
Personally, I always try to avoid reflection, as long as there is no other way to do your dirty work.