Animate newly added items in ListView

后端 未结 3 1987
梦如初夏
梦如初夏 2020-12-28 08:21

How can I animate newly added items in ListView?

I have a adapter and when I add new items in my list I say adapter.notifyDataSetChan

相关标签:
3条回答
  • 2020-12-28 09:02

    The official docs about animation in Android say that you can set an animation to trigger whenever the layout is changed, using android:animateLayoutChanges="true".

    Taken from: http://developer.android.com/training/animation/layout.html

    0 讨论(0)
  • 2020-12-28 09:03

    Adding this kind of animations is harder than I first thought of. There are two ways depending of the kind of animation you are trying to achieve.

    • Using LayoutAnimationController. There is an example in the API demos.

    • Animating each View:

    This is quite a hack but the only way I found to add an animation to ListView's children is the following:

    You can try notifying the adapter the id of the item you are willing to delete and call adapter.notifyDataSetChanged();. This will generate calls to the adapter's getView() method. Inside it you can do something like:

    if ( item.getId() == itemToRemove ) {
     //apply the animation
    }
    

    After the animation finished you can recall adapter.notifyDataSetChanged() to put everything in place.

    0 讨论(0)
  • 2020-12-28 09:15

    Animate each added element in the getView() method of your Custom Adapter.

    public View getView(int position, View convertView, ViewGroup parent) {
    
        View v = convertView;
    
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getActivity()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.simple_list_item_1, null);
        }
    
        ListData o = list.get(position);
        TextView tt = (TextView) v.findViewById(R.id.toptext);
    
        tt.setText(o.content);
    
        Log.d("ListTest", "Position : "+position);
        if(flag == false) {
            Animation animation = AnimationUtils
                    .loadAnimation(getActivity(), R.anim.slide_top_to_bottom);
            v.startAnimation(animation);
        }
        return v;
    }
    

    And thereby achieve the Animation.

    0 讨论(0)
提交回复
热议问题