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:
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 yourBaseAdapter
in its constructor; - Set your
ListView
to yourAnimationAdapter
; - Set your
AnimationAdapter
to 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 AnimationAdapter
s 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 AnimationAdapter
s don't suit your needs, you can implement your own AnimationAdapter
. Extend one of the following classes:
Have a look at the prepared AnimationAdapter
s for how to implement them.