Animate addition to android listview?

661 Views Asked by At

In my application, I am fetching data from a JSON feed every 30 seconds and displaying it in a listview. The data and listview are working just fine, though, when it updates, the application just jolts and doesn't look right. Is there any way that I can animate this transition? As it updates, the data is just moving up an index, which is stored in a listview. This is my code to update my listview

list = (ListView) rootView.findViewById(R.id.list);
LiveAdapter adapter = new LiveAdapter(LiveStreamFragment.this.getActivity(), oslist, LiveStreamFragment.this, list);
list.setAdapter(adapter);
1

There are 1 best solutions below

0
On

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

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

        View v = convertView;
       // Your code ..............
       if(flag == false) {
           Animation animation =  AnimationUtils.loadAnimation(getActivity(), R.anim.slide_top_to_bottom);
           v.startAnimation(animation);
         }
        return v;
    }

LayoutTransition (introduced in Android 3.0) continues to provide functionality that makes some kinds of animations easier, specifically when adding, removing, hiding, and showing views. For example, either this snippet in a layout file:

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"></ListView>

Animating changes with a ListView is a bit trickier than just setting the layout changes flag because of the view recycling that goes on with the Adapter. There is a series of DevBytes articles that Chet Haase has done explaining the issues and how to do this type of thing correctly here. Here is, I think, probably the most relevant one to your predicament.

DevBytes: ListView Animations