添加带有淡入淡出动画的ActionBar项

添加带有淡入淡出动画的ActionBar项

本文介绍了添加带有淡入淡出动画的ActionBar项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager,其中一个属于它的片段中有一个自己的ActionBar项,因此,当您滑动到该页面时,该项会出现在ActionBar中,而滑到另一页时,该项就会消失.

I have a ViewPager where one of its belonging fragments has its own ActionBar item, so that when you slide to that page the item comes forth in the ActionBar, slide to another page the item goes away.

我希望通过淡入/淡出动画来实现这一点-但不知道如何做.

I would like this to happen with a fade-in/fade-out animation - but don't know how.

我尝试了以下方法.但这给了我itemView.startAnimation(fade_in);

I've tried the following. But it gives me a NullPointerExecption on itemView.startAnimation(fade_in);

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.intruders_list, menu);

        // Setup animation
        Animation fade_in = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
        fade_in.setInterpolator(new AccelerateInterpolator());
        fade_in.setDuration(250);

        // Animate
        MenuItem deleteItem = menu.findItem(R.id.action_delete);
        View itemView = deleteItem.getActionView();
        itemView.startAnimation(fade_in); // NPE HERE

        super.onCreateOptionsMenu(menu, inflater);
}

推荐答案

之所以得到NullPointerException,是因为您试图获取未设置任何动作视图. getActionView()在您的情况下返回null.

You are getting a NullPointerException because you are trying to get an action view where none is set. getActionView() is returning null in you case.

要解决此问题,您需要使用deleteItem.setActionView(R.layout.layout_action_view);设置一个.
或者,您可以在intruders_list.xml中的项目上使用android:actionLayout="@layout/layout_action_view"进行设置.

To solve this issue you need to set one with deleteItem.setActionView(R.layout.layout_action_view);.
Alternatively you can set it with android:actionLayout="@layout/layout_action_view" on your item in intruders_list.xml.

layout_action_view.xml可能看起来像这样简单:

layout_action_view.xml could look as simple as this:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
   style="?android:attr/actionButtonStyle"
   android:id="@+id/iv_action"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@android:drawable/ic_menu_rotate" />

这篇关于添加带有淡入淡出动画的ActionBar项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:39