ListView-Animations提供一系列ListView显示的效果,也支持拖拽删除,长按添加的操作。
cards 卡牌的 listview 效果:
先附上listview的layout:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_googlecards_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e2e2e2"
android:clipToPadding="false"
android:divider="@null"
android:dividerHeight="16dp"
android:fadingEdge="none"
android:padding="16dp"
android:layout_marginTop="@dimen/paddingTop"
android:scrollbarStyle="outsideOverlay" />
ListView的item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/card_background_white"
android:orientation="vertical"
android:padding="16dp" >
<TextView
android:id="@+id/activity_googlecards_card_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<ImageView
android:id="@+id/activity_googlecards_card_imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</LinearLayout>
实现的Activity类GoogleCardsActivity:
public class GoogleCardsActivity extends BaseActivity implements OnDismissCallback {
private GoogleCardsAdapter mGoogleCardsAdapter;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("setContentView start");
setContentView(R.layout.activity_googlecards);
System.out.println("setContentView end");
ListView listView = (ListView) findViewById(R.id.activity_googlecards_listview);
mGoogleCardsAdapter = new GoogleCardsAdapter(this);
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(new SwipeDismissAdapter(mGoogleCardsAdapter, this)); /* 实现从底部渐入的效果的设配 也可以直接=new SwingBottomInANIAMtionAdapter(mGoogleCardsAdapter.this); */
swingBottomInAnimationAdapter.setInitialDelayMillis(300); /* 刚开始进入的延长时间 */
swingBottomInAnimationAdapter.setAbsListView(listView);
listView.setAdapter(swingBottomInAnimationAdapter);
mGoogleCardsAdapter.addAll(getItems());
}
private ArrayList<Integer> getItems() {
ArrayList<Integer> items = new ArrayList<Integer>();
for (int i = 0; i < 100; i++) {
items.add(i);
}
return items;
}
@Override
public void onDismiss(final AbsListView listView, final int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mGoogleCardsAdapter.remove(position);
}
}
private static class GoogleCardsAdapter extends ArrayAdapter<Integer> { /* 一个简单的ArrayAdapter */
private final Context mContext;
private final LruCache<Integer, Bitmap> mMemoryCache;
public GoogleCardsAdapter(final Context context) {
mContext = context;
final int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
@Override
protected int sizeOf(final Integer key, final Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
};
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
ViewHolder viewHolder;
View view = convertView;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.activity_googlecards_card, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.activity_googlecards_card_textview);
view.setTag(viewHolder);
viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView.setText("This is card " + (getItem(position) + 1));
setImageView(viewHolder, position);
return view;
}
private void setImageView(final ViewHolder viewHolder, final int position) {
int imageResId;
switch (getItem(position) % 5) {
case 0:
imageResId = R.drawable.img_nature1;
break;
case 1:
imageResId = R.drawable.img_nature2;
break;
case 2:
imageResId = R.drawable.img_nature3;
break;
case 3:
imageResId = R.drawable.img_nature4;
break;
default:
imageResId = R.drawable.img_nature5;
}
Bitmap bitmap = getBitmapFromMemCache(imageResId);
if (bitmap == null) {
bitmap = BitmapFactory.decodeResource(mContext.getResources(), imageResId);
addBitmapToMemoryCache(imageResId, bitmap);
}
viewHolder.imageView.setImageBitmap(bitmap);
}
private void addBitmapToMemoryCache(final int key, final Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
private Bitmap getBitmapFromMemCache(final int key) {
return mMemoryCache.get(key);
}
private static class ViewHolder {
TextView textView;
ImageView imageView;
}
}
}
GridView 网格样式的ListView 效果:(和上面的Cards差不多,使用起来很简单,哈哈)
layout:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_gridview_gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:paddingTop="@dimen/paddingTop" >
</GridView>
Activity:
public class GridViewActivity extends ActionBarActivity {
@SuppressLint("InlinedApi")
@Override
protected void onCreate(final Bundle savedInstanceState) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
// }
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
GridView gridView = (GridView) findViewById(R.id.activity_gridview_gv);
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(new MyAdapter(this, getItems()));
swingBottomInAnimationAdapter.setAbsListView(gridView);
swingBottomInAnimationAdapter.setInitialDelayMillis(300);
gridView.setAdapter(swingBottomInAnimationAdapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private ArrayList<Integer> getItems() {
ArrayList<Integer> items = new ArrayList<Integer>();
for (int i = 0; i < 100; i++) {
items.add(i);
}
return items;
}
private static class MyAdapter extends ArrayAdapter<Integer> {
private final Context mContext;
private final LruCache<Integer, Bitmap> mMemoryCache;
public MyAdapter(final Context context, final List<Integer> list) {
super(list);
mContext = context;
final int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
@Override
protected int sizeOf(final Integer key, final Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
};
}
@Override
public View getView(final int position, final View convertView, final ViewGroup viewGroup) {
ImageView imageView = (ImageView) convertView;
if (imageView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
int imageResId;
switch (getItem(position) % 5) {
case 0:
imageResId = R.drawable.img_nature1;
break;
case 1:
imageResId = R.drawable.img_nature2;
break;
case 2:
imageResId = R.drawable.img_nature3;
break;
case 3:
imageResId = R.drawable.img_nature4;
break;
default:
imageResId = R.drawable.img_nature5;
}
Bitmap bitmap = getBitmapFromMemCache(imageResId);
if (bitmap == null) {
bitmap = BitmapFactory.decodeResource(mContext.getResources(), imageResId);
addBitmapToMemoryCache(imageResId, bitmap);
}
imageView.setImageBitmap(bitmap);
return imageView;
}
private void addBitmapToMemoryCache(final int key, final Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
private Bitmap getBitmapFromMemCache(final int key) {
return mMemoryCache.get(key);
}
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
AppearanceExamplesActivity:
public class AppearanceExamplesActivity extends MyListActivity implements OnNavigationListener {
private BaseAdapter mAdapter;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyAdapter(this, getItems());
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(new AnimSelectionAdapter(), (OnNavigationListener) AppearanceExamplesActivity.this);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
private void setAlphaAdapter() {
AnimationAdapter animAdapter = new AlphaInAnimationAdapter(mAdapter);
animAdapter.setAbsListView(getListView());
getListView().setAdapter(animAdapter);
}
private void setLeftAdapter() {
AnimationAdapter animAdapter = new SwingLeftInAnimationAdapter(mAdapter);
animAdapter.setAbsListView(getListView());
getListView().setAdapter(animAdapter);
}
private void setRightAdapter() {
AnimationAdapter animAdapter = new SwingRightInAnimationAdapter(mAdapter); /* 从右渐进 */
animAdapter.setAbsListView(getListView());
getListView().setAdapter(animAdapter);
}
private void setBottomAdapter() {
AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(mAdapter); /* 从底部渐进 */
animAdapter.setAbsListView(getListView());
getListView().setAdapter(animAdapter);
}
private void setBottomRightAdapter() {
AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(new SwingRightInAnimationAdapter(mAdapter)); /* 从底部和右边叠加,就是斜向上的效果啦 */
animAdapter.setAbsListView(getListView());
getListView().setAdapter(animAdapter);
}
private void setScaleAdapter() {
AnimationAdapter animAdapter = new ScaleInAnimationAdapter(mAdapter); /* 等待然后原位置出现 */
animAdapter.setAbsListView(getListView());
getListView().setAdapter(animAdapter);
}
@Override
public boolean onNavigationItemSelected(final int itemPosition, final long itemId) {
switch (itemPosition) {
case 0:
setAlphaAdapter();
return true;
case 1:
setLeftAdapter();
return true;
case 2:
setRightAdapter();
return true;
case 3:
setBottomAdapter();
return true;
case 4:
setBottomRightAdapter();
return true;
case 5:
setScaleAdapter();
return true;
default:
return false;
}
}
/* Non-ListViewAnimations related stuff below */
private static class MyAdapter extends ArrayAdapter<Integer> {
private final Context mContext;
public MyAdapter(final Context context, final ArrayList<Integer> items) {
super(items);
mContext = context;
}
@Override
public long getItemId(final int position) {
return getItem(position).hashCode();
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
TextView tv = (TextView) convertView;
if (tv == null) {
tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
}
tv.setText("This is row number " + getItem(position));
return tv;
}
}
private class AnimSelectionAdapter extends ArrayAdapter<String> {
public AnimSelectionAdapter() {
addAll("Alpha", "Left", "Right", "Bottom", "Bottom right", "Scale");
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
TextView tv = (TextView) convertView;
if (tv == null) {
tv = (TextView) LayoutInflater.from(AppearanceExamplesActivity.this).inflate(android.R.layout.simple_list_item_1, parent, false);
}
tv.setText(getItem(position));
return tv;
}
}
}
list_row.xml:(一个普通的TextView)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:minHeight="48dp"
android:textSize="20sp" />
调用非常简单效果又好:
BaseAdaapter mAdapter = new 你自己的设配器;
AnimationAdapter animAdapter = new ScaleInAnimationAdapter(mAdapter);
animAdapter.setAbsListView(getListView());
getListView().setAdapter(animAdapter);