How can I animate one single item (entry) after the List is already updated and drawn?

情到浓时终转凉″ 提交于 2020-01-01 19:17:31

问题


I have this main ListView (retrieved using this.getListView()) in a ListActivity properly populated from a SQLite database. A click on one of its items (entries) calls another activity A2 using startActivityForResult(). I would like to animate that SINGLE entry WHEN the user gets back to the ListActivity. (So I suppose I need to override the onActivityResult() method)

How can I animate a single entry once the window gained focus and after the list is updated using notifyDataSetChanged()?

I managed to animate the whole ListView using

getListView().setAnimation(anim)

and worked fine. However, when I do:

getListView().getChildAt(position).setAnimation(anim)

nothing happens.

EDIT: Here's my onActivityResult code

protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {

    super.onActivityResult(requestCode, resultCode, intent);

    if(resultCode==RESULT_OK && requestCode==SOME_REQUEST_CODE){

        Animation anim = new AlphaAnimation(1.0f,0.0f);  
        anim.setFillAfter(true);  
        anim.setDuration(400); 
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(7);
        // Just for testing, I chose the first item of the ListView (position=0)
        tr_adapter.getView(0, getListView().getChildAt(0), getListView()).setAnimation(anim);

        tr_adapter.notifyDataSetChanged(); //Nothing happens

    }

}

回答1:


Set a flag in your table and call notifyDataSetChanged() on your list adapter. Have getView() in your list adapter determine what to do (animate or not) based on the flag in your table. I'd post some sample code but I'm mobile.

EDIT


In your onActivityResult(), flag (with some value, boolean, 1/0, etc) the data point in your data set that you'd like to animate. Then call notifyDataSetChanged() on the adapter. Assuming you have a custom list adapter, have your getView() function flip the animation on or off (or default to off and flip it on given the flag).

public View getView (int position, View convertView, ViewGroup parent){

    // All the standard getView() stuff

    if (listitem.shouldAnimate())
        // animate
}


来源:https://stackoverflow.com/questions/14096044/how-can-i-animate-one-single-item-entry-after-the-list-is-already-updated-and

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