工具栏消失时 OnClickListener 不触发
OnClickListener not firing when Toolbar GONE
我有一个包含 Toolbar
的 activity 和一个包含 GridView
的 Fragment
。
这是 Activity
的布局:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v7.widget.Toolbar
android:id="@+id/home_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/abc_action_bar_default_height_material_custo"
android:background="@color/black">
<include layout="@layout/custom_action_bar"/>
</android.support.v7.widget.Toolbar>
</FrameLayout>
我的Fragment
被插入到content_frame布局中。
这是我的 Fragment
的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView"
android:paddingTop="@dimen/abc_action_bar_default_height_material_custo"
android:clipToPadding="false"/>
</RelativeLayout>
相当标准的东西。
我在滚动 GridView
时为 ToolBar
设置动画,以便使其从顶部开始 disappear/appear。
mShowAB = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_slide_in_top);
mHideAB = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_slide_out_top);
mShowAB.setFillAfter(true);
mHideAB.setFillAfter(true);
动画资源是Android的默认ActionBar
动画。
一切正常,除了当 ToolBar
不可见时,我没有在 ToolBar
最初定位的 GridView
上收到点击事件。
我也试图让 Toolbar
GONE 但它没有帮助。
mHideAB.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (mToolbar != null) {
mToolbar.setVisibility(View.GONE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
有人遇到同样的问题吗?你知道为什么以及如何解决这个问题吗?
谢谢
解法:
解决方案是使用 ObjectAnimator:
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mToolbar, "translationY", 0);
objectAnimator.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
objectAnimator.start();
你可以找到答案here,但由于旧动画系统的限制:)
我有一个包含 Toolbar
的 activity 和一个包含 GridView
的 Fragment
。
这是 Activity
的布局:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v7.widget.Toolbar
android:id="@+id/home_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/abc_action_bar_default_height_material_custo"
android:background="@color/black">
<include layout="@layout/custom_action_bar"/>
</android.support.v7.widget.Toolbar>
</FrameLayout>
我的Fragment
被插入到content_frame布局中。
这是我的 Fragment
的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView"
android:paddingTop="@dimen/abc_action_bar_default_height_material_custo"
android:clipToPadding="false"/>
</RelativeLayout>
相当标准的东西。
我在滚动 GridView
时为 ToolBar
设置动画,以便使其从顶部开始 disappear/appear。
mShowAB = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_slide_in_top);
mHideAB = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_slide_out_top);
mShowAB.setFillAfter(true);
mHideAB.setFillAfter(true);
动画资源是Android的默认ActionBar
动画。
一切正常,除了当 ToolBar
不可见时,我没有在 ToolBar
最初定位的 GridView
上收到点击事件。
我也试图让 Toolbar
GONE 但它没有帮助。
mHideAB.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (mToolbar != null) {
mToolbar.setVisibility(View.GONE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
有人遇到同样的问题吗?你知道为什么以及如何解决这个问题吗?
谢谢
解法:
解决方案是使用 ObjectAnimator:
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mToolbar, "translationY", 0);
objectAnimator.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
objectAnimator.start();
你可以找到答案here,但由于旧动画系统的限制:)