我们先做一些事先的准备代码:
1.首先是就是listview的item布局:listview_item.xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/tetle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:saveEnabled="true"
android:text="android标题"
android:textSize="16sp" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tetle"
android:layout_marginTop="5dp"
android:saveEnabled="true"
android:text="android测试内容显示"
android:textSize="16sp" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/content"
android:layout_marginTop="5dp"
android:saveEnabled="true"
android:text="2016-12-30"
android:textSize="16sp" />
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/content"
android:drawableLeft="@mipmap/ic_launcher"
android:drawablePadding="5dp"
android:gravity="center"
android:text="18720824645" />
</RelativeLayout>
2.一个bean文件:
/**
* Created by Administrator on 2016/12/30.
*
* @author diaobao-刘平
*/
public class Bean {
private String title;
private String content;
private String time;
private String phone;
public Bean() {
}
public Bean(String title, String content, String time, String phone) {
this.title = title;
this.content = content;
this.time = time;
this.phone = phone;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
好了 好了 这里的listview呢! 就自己写了啊 前期工作那就先这些吧 我们进入正题
一、我们先来看看我们通常adapter适配器的使用方法,目的就是做个对比
/**
* Created by Administrator on 2016/12/30.
* 传统listview的适配器的使用
* @author diaobao-刘平
*/
public class MyAdapter extends BaseAdapter {
private List<Bean> datas = new ArrayList<>();
private LayoutInflater mLayoutInflater;
public MyAdapter(Context context, List<Bean> datas) {
this.datas = datas;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int i) {
return datas.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View contextView, ViewGroup viewGroup) {
Bean bean = (Bean) getItem(i);
ViewHolder holder = null;
if (contextView == null) {
contextView = mLayoutInflater.inflate(R.layout.listview_item, viewGroup, false);
holder = new ViewHolder(contextView);
contextView.setTag(holder);
} else {
holder = (ViewHolder) contextView.getTag();
}
holder.tetle.setText(bean.getTitle());
holder.time.setText(bean.getTime());
holder.content.setText(bean.getContent());
holder.phone.setText(bean.getPhone());
return contextView;
}
private class ViewHolder {
TextView tetle;
TextView content;
TextView time;
TextView phone;
public ViewHolder(View contextView) {
tetle = (TextView) contextView.findViewById(R.id.tetle);
time = (TextView) contextView.findViewById(R.id.time);
content = (TextView) contextView.findViewById(R.id.content);
phone = (TextView) contextView.findViewById(R.id.phone);
}
}
}
二、然而我们现在呢来抽取一些我们常用的方法
1.首先呢我们就来抽取一下VivewHolder的类:
/**
* Created by Administrator on 2016/12/30.
* 在使用adapter时使用的viewholder方法的抽取类-公共类
*
* @author diaobao-刘平
*/
public class ViewHolder {
private SparseArray<View> mVIews;
private View mConvertView;
private int mPosition;
/**
* 初始化ViewHolder内容
*
* @param context 上下文对象
* @param parent ViewGroup的对象-在baseadapter中的getView()中传入
* @param layoutId layout的id-就是listview的item
* @param ponstion ponstion的对象-在baseadapter中的getView()中传入
*/
public ViewHolder(Context context, ViewGroup parent, int layoutId, int ponstion) {
this.mPosition = ponstion;
this.mVIews = new SparseArray<View>();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
mConvertView.setTag(this);
}
/**
* @param context 上下文对象
* @param convertView view对象
* @param parent ViewGroup的对象-在baseadapter中的getView()中传入
* @param layoutId layout的id-就是listview的item
* @param position ponstion的对象-在baseadapter中的getView()中传入
* @return
*/
public static ViewHolder Get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new ViewHolder(context, parent, layoutId, position);
} else {
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.mPosition = position;
return holder;
}
}
/**
* 根据view的id获取控件
*
* @param viewId view的id
* @param <T>
* @return
*/
public <T extends View> T getVIew(int viewId) {
View view = mVIews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mVIews.put(viewId, view);
}
return (T) view;
}
public View getConvertView() {
return mConvertView;
}
/**
* 我们来偷偷懒 设置常用textview的设置值 这里还可以设置自己想设置的哦
* @param viewId textivew的id
* @param text 要设置的值内容
* @return
*/
public ViewHolder SetText(int viewId, String text) {
TextView tv = getVIew(viewId);
tv.setText(text);
return this;
}
}
2、为了再方便点我们把适配器也抽取了吧:命名一个commonAdapter类
/**
* Created by Administrator on 2016/12/30.
*提取出来的adapter的公共类
* @author diaobao-刘平
*/
public abstract class CommonAdapter<T> extends BaseAdapter {
protected List<T> mDatas;
protected Context mContext;
protected LayoutInflater mInflater;
public CommonAdapter(Context context, List<T> mDatas) {
this.mContext = context;
this.mDatas = mDatas;
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int i) {
return mDatas.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
//在这里应为getVIew方法是不一样的所以我们得把它公共出去 在这里呢你要是再懒点还可以抽取的哦 自己看着办啊
@Override
public abstract View getView(int i, View view, ViewGroup viewGroup);
}
3、一系列抽取之后我们在来比比看我们现在的适配器吧:第一个MyVivwHolderAdapter类
**
* Created by Administrator on 2016/12/30.
* 使用ViewHolder的适配器的使用
*
* @author diaobao-刘平
*/
public class MyViewHolderAdapter extends CommonAdapter<Bean> {
public MyViewHolderAdapter(Context context, List<Bean> datas) {
super(context,datas);
}
@Override
public View getView(int i, View contextView, ViewGroup viewGroup) {
Bean bean = (Bean) getItem(i);
ViewHolder holder = ViewHolder.Get(mContext, contextView, viewGroup, R.layout.listview_item, i);
holder.SetText(R.id.tetle,bean.getTitle());
holder.SetText(R.id.time,bean.getTime());
holder.SetText(R.id.phone,bean.getPhone());
holder.SetText(R.id.content,bean.getContent());
return holder.getConvertView();
}
}
是不是简单好多了呀 最后结果呢 我就不截图了哦 你们自己运行看看吧 下面给出代码哦
源码下载:源码在这哦