listview超级优化的adapter

把ViewHoler抽取封装一个类,把BaseAdapter抽取封装一个类,CommentAdapter。大大优化代码结构,简化代码,

MainActivity.java

======================

package com.example.mylistview;


import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ListView;


import com.example.mylistviewbaseAdapter.bean.Bean;
import com.example.mylistviewbaseAdapter.util.CommentAdapter;
import com.example.mylistviewbaseAdapter.util.ViewHolder;


public class MainActivity extends Activity {


private ListView mListView;
private List<Bean> lists;


// private MyAdapter adapter;// 传统的adapter


// private MyAdapterWithComment mMyAdapterWithComment;// 万能适配器的adapter,通用的adapter


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
initView();
initBean();
}


private void initBean() {
lists = new ArrayList<Bean>();
Bean mBean = null;
for (int i = 0; i < 20; i++) {
mBean = new Bean();
mBean.setTitleStr("我的测试ListView" + i);
mBean.setDateStr("2016-" + i);
mBean.setDesStr("ListView测试" + i);
lists.add(mBean);
}
initData();
}


private void initData() {
runOnUiThread(new Runnable() {


@Override
public void run() {
// if (mMyAdapterWithComment == null) {
// adapter = new MyAdapter(getApplicationContext(), lists);//传统的apapter
// mMyAdapterWithComment = new MyAdapterWithComment(getApplicationContext(), R.layout.item_listview, lists);//
// 万能适配器,通用的。
// mListView.setAdapter(mMyAdapterWithComment);
mListView.setAdapter(new CommentAdapter<Bean>(MainActivity.this, R.layout.item_listview, lists) {// 更加优化


@Override
public void convert(ViewHolder holder, final Bean bean) {
holder.setText(R.id.tv_title, bean.getTitleStr()).setText(R.id.tv_desc, bean.getDesStr()).setText(R.id.tv_date, bean.getDateStr());
// ((TextView) holder.getView(R.id.tv_title)).setText(bean.getTitleStr());
// ((TextView) holder.getView(R.id.tv_desc)).setText(bean.getDesStr());
// ((TextView) holder.getView(R.id.tv_date)).setText(bean.getDateStr());
final CheckBox cBox = holder.getView(R.id.cb);
cBox.setChecked(bean.isChecked());
cBox.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
bean.setChecked(cBox.isChecked());
}
});
}


});
// } else {
// mMyAdapterWithComment.notifyDataSetChanged();
// }
}
});
}


private void initView() {
mListView = (ListView) findViewById(R.id.listView);
}


}


MyAdapterWithComment.java

========================

package com.example.mylistview;


import java.util.List;


import android.content.Context;


import com.example.mylistviewbaseAdapter.bean.Bean;
import com.example.mylistviewbaseAdapter.util.CommentAdapter;
import com.example.mylistviewbaseAdapter.util.ViewHolder;


public class MyAdapterWithComment extends CommentAdapter<Bean> {


public MyAdapterWithComment(Context context, int layoutId, List<Bean> datas) {
super(context, mLayoutId, datas);
}


@Override
public void convert(ViewHolder holder, Bean bean) {
// ((TextView) holder.getView(R.id.tv_title)).setText(bean.getTitleStr());
holder.setText(R.id.tv_title, bean.getTitleStr()).setText(R.id.tv_desc, bean.getDesStr()).setText(R.id.tv_date, bean.getDateStr());
// ((TextView) holder.getView(R.id.tv_desc)).setText(bean.getDesStr());
// ((TextView) holder.getView(R.id.tv_date)).setText(bean.getDateStr());
}
}


ViewHolder.java

=======================

package com.example.mylistviewbaseAdapter.util;


import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


public class ViewHolder {


private SparseArray<View> mView;
private View mContextView;
private int mPosition;


public ViewHolder(Context context, int layoutId, ViewGroup parent, int position) {
this.mPosition = position;
this.mView = new SparseArray<View>();
this.mContextView = LayoutInflater.from(context).inflate(layoutId, parent, false);
mContextView.setTag(this);
}


public View getContextView() {
return mContextView;
}


public int getmPosition() {
return mPosition;
}


public static ViewHolder getViewHolder(Context context, View convertView, int layoutId, ViewGroup parent, int position) {
if (convertView == null) {
return new ViewHolder(context, layoutId, parent, position);
} else {
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.mPosition = position;
return holder;
}
}


public <T extends View> T getView(int viewId) {
View view = mView.get(viewId);
if (view == null) {
view = mContextView.findViewById(viewId);
mView.put(viewId, view);
}
return (T) view;
}


public ViewHolder setText(int viewId, String text) {
TextView tv = getView(viewId);
tv.setText(text);
return this;
}


public ViewHolder setImageResource(int viewId, int resId) {
ImageView view = getView(viewId);
view.setImageResource(resId);
return this;
}


public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
ImageView view = getView(viewId);
view.setImageBitmap(bitmap);
return this;
}


public ViewHolder setImageUrl(int viewId, String url) {
ImageView view = getView(viewId);
// ImageLoader.getInstance().loadImg(view,url);
return this;
}


}



CommentAdapter.java

=============================


package com.example.mylistviewbaseAdapter.util;


import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


public abstract class CommentAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mDatas;
protected LayoutInflater mInflater;
protected static int mLayoutId;


public CommentAdapter(Context context, int layoutId, List<T> datas) {
this.mContext = context;
this.mLayoutId = layoutId;
mInflater = LayoutInflater.from(context);
this.mDatas = datas;
}


@Override
public int getCount() {
return mDatas.size();
}


@Override
public T getItem(int position) {
return mDatas.get(position);
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.getViewHolder(mContext, convertView, mLayoutId, parent, position);
convert(holder, getItem(position));
return holder.getContextView();
}


public abstract void convert(ViewHolder holder, T t);


}


Bean.java

===========================


package com.example.mylistviewbaseAdapter.bean;


public class Bean {
private String titleStr;
private String desStr;
private String dateStr;
private boolean isChecked;


public boolean isChecked() {
return isChecked;
}


public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}


public String getTitleStr() {
return titleStr;
}


public void setTitleStr(String titleStr) {
this.titleStr = titleStr;
}


public String getDesStr() {
return desStr;
}


public void setDesStr(String desStr) {
this.desStr = desStr;
}


public String getDateStr() {
return dateStr;
}


public void setDateStr(String dateStr) {
this.dateStr = dateStr;
}


@Override
public String toString() {
return "Bean [titleStr=" + titleStr + ", desStr=" + desStr + ", dateStr=" + dateStr + "]";
}


}


CSDN下载地址:http://download.csdn.net/detail/renhaijuniryfe/9494132


gethub下载地址:https://github.com/renhaijun/MyListView201604180034-Adapter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yayayaiii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值