Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Appearance Animations

Niek Haarman edited this page Feb 22, 2014 · 4 revisions

If you want to animate your list items when they first appear, you can follow this guide.

Quick-start

To use one of the default animations for your list items, you can use one of the classes in the .swinginadapters.prepared package:

  • AlphaInAnimationAdapter
  • ScaleInAnimationAdapter
  • SwingBottomInAnimationAdapter
  • SwingLeftInAnimationAdapter
  • SwingRightInAnimationAdapter

Steps for using these classes:

  • Implement your own BaseAdapter, or reuse an existing one;
  • Create a new AnimationAdapter of your choice, providing your BaseAdapter in its constructor;
  • Set your ListView to your AnimationAdapter;
  • Set your AnimationAdapter to your ListView.

In code:

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	MyListAdapter mAdapter = new MyListAdapter(this, getItems());
	SwingRightInAnimationAdapter swingRightInAnimationAdapter = new SwingRightInAnimationAdapter(mAdapter);

	// Assign the ListView to the AnimationAdapter and vice versa
	swingRightInAnimationAdapter.setAbsListView(getListView());
	getListView().setAdapter(swingRightInAnimationAdapter);
}

private class MyListAdapter extends com.nhaarman.listviewanimations.ArrayAdapter<String> {

	private Context mContext;

	public MyListAdapter(Context context, ArrayList<String> items) {
		super(items);
		mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView tv = (TextView) convertView;
		if (tv == null) {
			tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
		}
		tv.setText(getItem(position));
		return tv;
	}
}

Advanced

Multiple AnimationAdapters

You can use multiple AnimationAdapters on your ListView. Just stack them on top of eachother:

MyListAdapter mAdapter = new MyListAdapter(this, getItems());
SwingRightInAnimationAdapter swingRightInAnimationAdapter = new SwingRightInAnimationAdapter(mAdapter);
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(swingRightInAnimationAdapter);

swingRightInAnimationAdapter.setAbsListView(getListView());
getListView().setAdapter(swingBottomInAnimationAdapter);

This particular example makes the views animate diagonally in from bottom right.

Custom AnimationAdapters

If the prepared AnimationAdapters don't suit your needs, you can implement your own AnimationAdapter. Extend one of the following classes:

Have a look at the prepared AnimationAdapters for how to implement them.