How can I animate one single item (entry) after th

2019-04-15 12:25发布

问题:

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
}