Add ActionBar Items with a fade animation

萝らか妹 提交于 2019-12-12 23:07:54

问题


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.

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);  
}

回答1:


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.

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 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" />


来源:https://stackoverflow.com/questions/21434704/add-actionbar-items-with-a-fade-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!