item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="48dp"android:gravity="center"android:text="万能适配器测试"android:textSize="18sp" />
</LinearLayout>
listview加载数据
private void commonAdapterTest() {ListView listView = (ListView) findViewById(R.id.listview);List<String> datas = new ArrayList<>();for (int i = 0; i < 18; i++) {datas.add("万能适配器测试" + i);}listView.setAdapter(new CommonAdapter<String>(this, datas, R.layout.item) {@Overrideprotected void convertView(View item, String s) {TextView textView = CommonViewHolder.get(item, R.id.textView);textView.setText(s);}});
}
两个工具类优化listview
public abstract class CommonAdapter<T> extends BaseAdapter {private Context context;private List<T> datas;private int layoutId;public CommonAdapter(Context context, List<T> datas, int layoutId) {this.context = context;this.datas = datas;this.layoutId = layoutId;}@Overridepublic int getCount() {return datas == null ? 0 : datas.size();}@Overridepublic T getItem(int position) {return datas.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {Log.d("listview", "---------LayoutInflater()-----------" + position);convertView = LayoutInflater.from(context).inflate(layoutId, null);}else{}
// Log.d("listview", "---------getView()-----------" + position);T t = getItem(position);convertView(convertView, t);return convertView;}/*** 局部更新数据,调用一次getView()方法;Google推荐的做法** @param listView 要更新的listview* @param position 要更新的位置*/public void notifyDataSetChanged(ListView listView, int position) {/**第一个可见的位置**/int firstVisiblePosition = listView.getFirstVisiblePosition();/**最后一个可见的位置**/int lastVisiblePosition = listView.getLastVisiblePosition();/**在看见范围内才更新,不可见的滑动后自动会调用getView方法更新**/if (position >= firstVisiblePosition && position <= lastVisiblePosition) {/**获取指定位置view对象**/View view = listView.getChildAt(position - firstVisiblePosition);getView(position, view, listView);}}/*** 需要去实现的对item中的view的设置操作** @param item* @param t*/protected abstract void convertView(View item, T t);}
2222222222222222222222222222
public class CommonViewHolder {/*** @param view 所有缓存View的根View* @param id 缓存View的唯一标识* @return*/public static <T extends View> T get(View view, int id) {SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();//如果根view没有用来缓存View的集合if (viewHolder == null) {viewHolder = new SparseArray<View>();view.setTag(viewHolder);//创建集合和根View关联}View chidlView = viewHolder.get(id);//获取根View储存在集合中的孩纸if (chidlView == null) {//如果没有改孩纸//找到该孩纸chidlView = view.findViewById(id);viewHolder.put(id, chidlView);//保存到集合}return (T) chidlView;}
}
2、BaseAdapter使用
public class SampleAdapter extends BaseAdapter {private List<AreaEntity> mDataSet;private Context context;public SampleAdapter(Context context, List<AreaEntity> dataSet) {this.mDataSet = dataSet;this.context = context;}@Overridepublic int getCount() {return mDataSet.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}// 删除 打勾 全选public void cleckAll(int is_checked) { //全选 删除多少那里要删除全部for (int a = 0; a < mDataSet.size(); a++) {if (is_checked == a) {mDataSet.get(a).setIfsecect(true);} else {mDataSet.get(a).setIfsecect(false);}}notifyDataSetChanged();}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {convertView = View.inflate(context, R.layout.item_popup_car, null);TextView textView = (TextView) convertView.findViewById(R.id.text);textView.setText(mDataSet.get(position).getName());if (position == 1) {textView.setTextColor(context.getResources().getColor(R.color.derder));}if (mDataSet.get(position).isIfsecect()) {textView.setBackgroundColor(context.getResources().getColor(R.color.topbgokc));} else {textView.setBackgroundColor(context.getResources().getColor(R.color.xianlanbgc));}return convertView;}}
3、ArrayAdapter使用
item_view
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/tv_items"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="20dp"android:textSize="25sp"/></LinearLayout>
实现
private String[] mTitle = new String[]{"开启蓝牙", "关闭蓝牙", "设备列表", "接收数据", "已配对设备"};
mList.setAdapter(new ArrayAdapter<>(this, R.layout.item_list, R.id.tv_items, mTitle));