CommonBaseAdapter
package com.example.commonAdapter; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; public abstract class CommonBaseAdapter<T> extends BaseAdapter{ protected List<T> datas; protected Context context; protected int resourceId; public CommonBaseAdapter(Context context, List<T> datas, int resourceId) { this.datas = datas; this.context = context; this.resourceId = resourceId; } @Override public int getCount() { return datas.size(); } @Override public T getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent){ CommonViewHolder viewHolder = CommonViewHolder.getInstance(context, convertView, parent, resourceId); publishView(viewHolder,getItem(position)); return viewHolder.getConvertView(); } public abstract void publishView(CommonViewHolder viewHolder, T t); }
CommonViewHolder
package com.example.commonAdapter; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class CommonViewHolder { private View convertView; private SparseArray<View> views; public CommonViewHolder(Context context, ViewGroup parent, int resource) { this.views = new SparseArray<View>(); this.convertView = LayoutInflater.from(context).inflate(resource, parent,false); this.convertView.setTag(this); } public static CommonViewHolder getInstance(Context context,View convertView,ViewGroup parent,int resource){ if (convertView==null) { return new CommonViewHolder(context,parent,resource); }else{ return (CommonViewHolder) convertView.getTag(); } } public View getConvertView() { return convertView; } /** * viewholder.textView = convertView.findViewById(id); */ public <T extends View> T getView(int id){ View view = views.get(id); if (view ==null) { view = convertView.findViewById(id); views.put(id, view); } return (T) view; } public CommonViewHolder setText(int id,CharSequence text){ TextView textView = getView(id); textView.setText(text); return this; } }
使用事例
package com.example.commonAdapter; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class MainActivity extends Activity { private List<User> datas; private MyAdapter adapter; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); initDatas(); adapter = new MyAdapter(this, datas, R.layout.item); listView.setAdapter(adapter); } private void initDatas() { datas = new ArrayList<User>(); User user = null; for(int i = 0 ;i<30;i++){ user = new User("name"+i, false); datas.add(user); } } }
package com.example.commonAdapter; import java.util.List; import android.content.Context; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.TextView; public class MyAdapter extends CommonBaseAdapter<User> { public MyAdapter(Context context, List<User> datas,int resourceId) { super(context, datas, resourceId); } @Override public void publishView(CommonViewHolder viewHolder, final User user) { viewHolder.setText(R.id.name,user.getName()); CheckBox box = viewHolder.getView(R.id.cb); box.setChecked(user.isChecked()); box.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { boolean checked = !user.isChecked(); user.setChecked(checked); } }); } }
package com.example.commonAdapter; public class User { private String name; private boolean isChecked; public User(String name, boolean isChecked) { this.name = name; this.isChecked = isChecked; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isChecked() { return isChecked; } public void setChecked(boolean isChecked) { this.isChecked = isChecked; } }