Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding animation to a List View in Android

I want to animate the items of the list view. At Present i am applying the Transition Animation on the list items whenever new items are added. But this is not the animation i want to achieve. I want that when a new item is added in the list view at that time the whole List view move a place down to make way for the newly added item.

Currently the code i am using is :

set = new AnimationSet(true);

    animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(50);
    set.addAnimation(animation);

    animation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
        Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
    );
    animation.setDuration(150);
    set.addAnimation(animation);

    LayoutAnimationController controller = new LayoutAnimationController(set, 1.0f);
    l.setLayoutAnimation(controller);
    l.setAdapter(listAdaptor);

And then while adding items through button onClick

    l.startLayoutAnimation();

Any other suggestions to achieve such animation.

like image 740
ASH Avatar asked Jan 17 '12 11:01

ASH


People also ask

What is transition animation in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.

How is it possible to view a list in Android?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.

What is Property animation in Android?

A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.


1 Answers

I got the Solution to this. I animate each added element in the getView method of my 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 achieved the animation as i had stated for.

like image 139
ASH Avatar answered Nov 15 '22 17:11

ASH