调用公共方法ViewHolder和Adapter
第一中优化的方法
在Adapter中,以后ViewHolder就调用一下方法
package com.wwl.canvas.adapter; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class ViewHolder { private SparseArray<View> mViews; private int mPositon; private View mConvertView; public ViewHolder(Context context, ViewGroup parent, int layoutId, int position) { this.mPositon = position; this.mViews = new SparseArray<View>(); mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false); mConvertView.setTag(this); } 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.mPositon =position; return holder; } } /** * 通过iewId获取控件 * @param viewId * @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 getmConvertView() { return mConvertView; } }
然后Adapter继承CommAdapter<T>,如下方法
package com.wwl.canvas.adapter; 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 CommonAdapter<T> extends BaseAdapter { protected Context mContext; protected List<T> mDatas; protected LayoutInflater inflater; public CommonAdapter(Context context,List<T> datas){ this.mContext =context; this.mDatas =datas; inflater =LayoutInflater.from(context); } public List<T> getList(){
return mDatas;
}
public void appendToList(List<T> list) {
if (list == null) {
return;
}
mDatas.addAll(list);
notifyDataSetChanged();
}
public void appendToTopList(List<T> list) {
if (list == null) {
return;
}
mDatas.addAll(0, list);
notifyDataSetChanged();
}
public void appendT(T object) {
if (object == null) {
return;
}
mDatas.add(object);
notifyDataSetChanged();
}
public void removeT(T object) {
if (object == null) {
return;
}
mDatas.remove(object);
notifyDataSetChanged();
}
public void clear() {
mDatas.clear();
notifyDataSetChanged();
}
@Override public int getCount() { // TODO Auto-generated method stub return mDatas.size()>0?mDatas.size():0; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mDatas.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public abstract View getView(int position, View convertView, ViewGroup parent); }
最后查看Adapter中的操作方法:
public class MyAdapter extends CommonAdapter<Bean>{ public MyAdapter(Context context,List<Bean> mDatas) { super(context, mDatas); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = ViewHolder.get(mContext, convertView, parent, R.layout.list_item, position); Bean bean = mDatas.get(position); ((TextView)holder.getView(R.id.id_title)).setText(bean.getTitle()); ((TextView)holder.getView(R.id.id_desc)).setText(bean.getDesc()); ((TextView)holder.getView(R.id.id_time)).setText(bean.getTime()); ((TextView)holder.getView(R.id.id_phone)).setText(bean.getPhone()); return holder.getmConvertView(); } }
第二种优化的方法(在第一种的基础上继续优化)
ViewHolder类:
package com.wwl.canvas.adapter; import com.nostra13.universalimageloader.core.ImageLoader; 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> mViews; private int mPositon; private View mConvertView; public ViewHolder(Context context, ViewGroup parent, int layoutId, int position) { this.mPositon = position; this.mViews = new SparseArray<View>(); mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false); mConvertView.setTag(this); } 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.mPositon =position; return holder; } } public int getPositon() { return mPositon; } /** * 通过iewId获取控件 * @param viewId * @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 getmConvertView() { return mConvertView; } /** * 如果Adapter是TextView ,设置TextView的值 * @param viewId * @param text * @return */ public ViewHolder setText(int viewId,String text){ TextView tv =getView(viewId); tv.setText(text); return this; } /** * 如果Adapter是ImageView ,设置图片1111111 * @param viewId * @param text * @return */ public ViewHolder setImageResource(int viewId,int resId){ ImageView view =getView(viewId); view.setImageResource(resId); return this; } /** * 如果Adapter是ImageView ,设置图片22222 * @param viewId * @param text * @return */ public ViewHolder setImageBitmap(int viewId,Bitmap bitmap){ ImageView view =getView(viewId); view.setImageBitmap(bitmap); return this; } /** * 如果Adapter是ImageView ,设置图片33333 * @param viewId * @param text * @return */ public ViewHolder setImageURL(int viewId,String url){ ImageView view =getView(viewId); ImageLoader.getInstance().displayImage(url,view); return this; } }
CommAdapter类:
package com.xyrr.android.adapter; import java.util.List; public abstract class CommonAdapter<T> extends BaseAdapter { protected Context mContext; protected List<T> mDatas; protected LayoutInflater inflater; protected int layoutId; public CommonAdapter(Context context,List<T> datas,int layoutId){ this.mContext =context; this.mDatas =datas; this.layoutId =layoutId; inflater =LayoutInflater.from(context); } public List<T> getList() { return mDatas; } public void appendToList(List<T> list) { if (list == null) { return; } mDatas.addAll(list); notifyDataSetChanged(); } public void appendToTopList(List<T> list) { if (list == null) { return; } mDatas.addAll(0, list); notifyDataSetChanged(); } public void appendT(T object) { if (object == null) { return; } mDatas.add(object); notifyDataSetChanged(); } public void removeT(T object) { if (object == null) { return; } mDatas.remove(object); notifyDataSetChanged(); } public void clear() { mDatas.clear(); notifyDataSetChanged(); } @Override public int getCount() { // TODO Auto-generated method stub return mDatas.size()>0?mDatas.size():0; } @Override public T getItem(int position) { // TODO Auto-generated method stub return mDatas.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent){ ViewHolders holder = ViewHolders.get(mContext, convertView, parent, layoutId, position); convert(holder, getItem(position)); return holder.getmConvertView(); }; public abstract void convert(ViewHolders holder,T t); }
MyAdapter类:
package com.wwl.canvas.adapter; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.wwl.canvas.R; import com.wwl.canvas.view.Bean; public class MyAdapter extends CommonAdapter<Bean> { public MyAdapter(Context context, List<Bean> mDatas) { super(context, mDatas,R.layout.list_item); } @Override public void convert(ViewHolder holder, Bean bean) { holder.setText(R.id.id_title, bean.getTitle()) .setText(R.id.id_desc, bean.getDesc()) .setText(R.id.id_time, bean.getTime()) .setText(R.id.id_phone, bean.getPhone()); // ((TextView)holder.getView(R.id.id_title)).setText(bean.getTitle()); // ((TextView)holder.getView(R.id.id_desc)).setText(bean.getDesc()); // ((TextView)holder.getView(R.id.id_time)).setText(bean.getTime()); // ((TextView)holder.getView(R.id.id_phone)).setText(bean.getPhone()); } }
Bean类:
package com.wwl.canvas.view; import java.io.Serializable; public class Bean implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String title; private String desc; private String time; private String phone; private boolean isChecked; public boolean isChecked() { return isChecked; } public void setChecked(boolean isChecked) { this.isChecked = isChecked; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } 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; } }
MainActivity类实现:
package com.wwl.canvas; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.CheckBox; import android.widget.ListView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.wwl.canvas.adapter.CommonAdapter; import com.wwl.canvas.adapter.ViewHolder; import com.wwl.canvas.view.Bean; public class MainActivity extends Activity { DisplayImageOptions options; private ListView listview; private List<Bean> mDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // mLuckyPan =new CanvasDemo(this); // setContentView(mLuckyPan); setContentView(R.layout.activity_main); options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_launcher) .showImageForEmptyUri(R.drawable.ic_launcher) .showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true) .cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build(); listview =(ListView) findViewById(R.id.id_list); getDatas(); listview.setAdapter(new CommonAdapter<Bean>(MainActivity.this,mDatas,R.layout.list_item) { private List<Integer> mPos = new ArrayList<Integer>(); @Override public void convert(final ViewHolder holder, final Bean bean) { // TODO Auto-generated method stub holder.setText(R.id.id_title, bean.getTitle()) .setText(R.id.id_desc, bean.getDesc()) .setText(R.id.id_time, bean.getTime()) .setText(R.id.id_phone, bean.getPhone()); final CheckBox cb = holder.getView(R.id.id_cb); //第一种方法在bean中添加boolean类型的值,然后用cb.setChecked(bean.isChecked());以及点击的时候bean.setChecked(cb.isChecked());就可以了 // cb.setChecked(bean.isChecked()); //第二种方法:在adapter中获取mPosition的get方法。然后下面这个方法OK了 cb.setChecked(false); if (mPos.contains(holder.getPositon())) { cb.setChecked(true); } cb.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // bean.setChecked(cb.isChecked()); if (cb.isChecked()) { mPos.add(holder.getPositon()); }else{ mPos.remove((Integer)holder.getPositon()); } } }); } }); } private void getDatas() { mDatas = new ArrayList<Bean>(); for (int i = 0; i < 15; i++) { Bean mBean =new Bean(); mBean.setTitle("标题----"+(i+1)); mBean.setDesc("正文----"+(i+1)); mBean.setTime("时间----"+(i+1)); mBean.setPhone("电话----"+(i+1)); mDatas.add(mBean); } } }
list_item布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" android:orientation="vertical" > <CheckBox android:id="@+id/id_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_alignParentRight="true"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/id_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="标题。。。。。。。。。。"/> <TextView android:id="@+id/id_desc" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="正文"/> <TextView android:id="@+id/id_time" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="时间。。。。。。。。。。"/> <TextView android:id="@+id/id_phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="电话号码。。。。。。。。。。"/> </LinearLayout> </RelativeLayout>
上一篇: IOS相关参数记录
下一篇: 对于ssl的证书加载和编程