ListView的MyBaseAdapter的封装
import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.lang.reflect.Array; import java.util.ArrayList; import cn.loaderman.holder.BaseHolder; import cn.loaderman.holder.HomeHolder; import cn.loaderman.utils.ItemAnimationUtils; import static android.icu.lang.UCharacter.GraphemeClusterBreak.T; /** * * 封装的思想: * 抽取基类: * 在基类中做的一般都是通用的事情 * 不通用的事情 * 1、父类不知道如何完成的事情,通过抽象方法来交给子类实现 * 2、父类不知道的数据类型,通过泛型来解决,泛型是自定义的一种不存在的类型,这种类型究竟是啥,在定义子类的时候确定 * 尖括号的位置 * 如果在自己的类名后面,代表定义了一个泛型 * 如果跟在父类的名称后面,代表确定父类所定义的泛型类型 * */ public abstract class MyBaseAdapter<T> extends BaseAdapter { private ArrayList<T> dataList; public MyBaseAdapter(ArrayList<T> dataList) { this.dataList = dataList; } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { BaseHolder holder = null; if(convertView == null) { holder = getHolder(position); } else { holder = (BaseHolder) convertView.getTag(); } holder.setData(dataList.get(position)); ItemAnimationUtils.startAnim(holder.convertView); return holder.convertView; } public abstract BaseHolder getHolder(int position); }
BaseHolder.java
import android.view.View; /** * 1、加载布局文件 convertView,布局文件的id * 2、初始化控件 TextView、convertView、Holder * 3、存储holder Holder、convertView * 4、刷新控件的数据 TextView、数据 */ public abstract class BaseHolder<T> { public View convertView; public T data; public BaseHolder() { convertView = initView(); convertView.setTag(this); } public void setData(T data) { if(data != null) { this.data = data; } refreshView(); } public abstract View initView() ; public abstract void refreshView();\ }
ItemAnimationUtils.java
public class ItemAnimationUtils { public static void startAnim(View view) { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"alpha",0.0f,1.0f); objectAnimator.setDuration(500); objectAnimator.start(); /*AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(500); alphaAnimation.setFillAfter(true);*/ //view.setScaleX(); /*view.setPivotX(0); view.setPivotY(0);*/ ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(view,"scaleX",0.5f,1.0f); objectAnimatorX.setDuration(500); objectAnimatorX.start(); ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(view,"scaleY",0.5f,1.0f); objectAnimatorY.setDuration(500); objectAnimatorY.start(); /* ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f,1.0f,0.5f,1.0f); scaleAnimation.setDuration(500); scaleAnimation.setFillAfter(true); AnimationSet animationSet = new AnimationSet(true); //设置动画的插入器 animationSet.setInterpolator(new OvershootInterpolator(2)); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(scaleAnimation); view.startAnimation(animationSet);*/ } }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!