This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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.
To use one of the default animations for your list items, you can use one of the classes in the .swinginadapters.prepared package:
AlphaInAnimationAdapterScaleInAnimationAdapterSwingBottomInAnimationAdapterSwingLeftInAnimationAdapterSwingRightInAnimationAdapter
Steps for using these classes:
- Implement your own
BaseAdapter, or reuse an existing one; - Create a new
AnimationAdapterof your choice, providing yourBaseAdapterin its constructor; - Set your
ListViewto yourAnimationAdapter; - Set your
AnimationAdapterto yourListView.
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;
}
}
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.
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.