先看效果:
具体实现:
1.CommonAdapter: import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.List; public abstract class CommonAdapter<T> extends BaseAdapter { private Context context; private List<T> list; private LayoutInflater inflater; private int itemLayoutId; public CommonAdapter(Context context, List<T> list, int itemLayoutId) { this.context = context; this.list = list; this.itemLayoutId = itemLayoutId; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list == null ? 0 : list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { CommonViewHolder holder = getViewHolder(position, convertView, parent); convert(holder, (T) getItem(position)); return holder.getConvertView(); } public abstract void convert(CommonViewHolder holder, T item); private CommonViewHolder getViewHolder(int position, View convertView, ViewGroup parent) { return CommonViewHolder.get(context, convertView, parent, itemLayoutId, position); } } 2.commonViewHolder: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; import com.bumptech.glide.Glide; public class CommonViewHolder { //现在对于int作为键的官方推荐用SparseArray替代HashMap private final SparseArray<View> views; private View convertView; private Context context; private CommonViewHolder(Context context, ViewGroup parent, int layoutId, int position) { this.context = context; this.views = new SparseArray<>(); this.convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false); convertView.setTag(this); } /** * 拿到一个ViewHolder对象 */ public static CommonViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) { if (convertView == null) { return new CommonViewHolder(parent.getContext(), parent, layoutId, position); } return (CommonViewHolder) convertView.getTag(); } /** * 通过控件的Id获取对于的控件,如果没有则加入views */ public <T extends View> T getView(int viewId) { View view = views.get(viewId); if (view == null) { view = convertView.findViewById(viewId); views.put(viewId, view); } return (T) view; } public View getConvertView() { return convertView; } //设置字符串 public CommonViewHolder setText(int viewId, String text) { TextView tv = getView(viewId); tv.setText(text); return this; } //设置图片 public CommonViewHolder setImageByUrl(int viewId, String url) { Glide.with(context).load(url).into((ImageView) getView(viewId)); return this; } public CommonViewHolder setImageResource(int viewId, int drawableId) { ImageView iv = getView(viewId); iv.setImageResource(drawableId); return this; } public CommonViewHolder setImageBitmap(int viewId, Bitmap bitmap) { ImageView iv = getView(viewId); iv.setImageBitmap(bitmap); return this; } }
3.MyData:
public class MyData { private String name; public MyData(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }4.MainActivity:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { final List<MyData> list = new ArrayList<>(); for (int i = 0; i < 50; i++) { list.add(new MyData("刘婕" + i + "")); } mListView.setAdapter(new CommonAdapter<MyData>(this, list, R.layout.item) { @Override public void convert(CommonViewHolder holder, MyData item) { holder.setText(R.id.text, item.getName()); holder.setImageResource(R.id.image,R.mipmap.ic_launcher); } }); } private void initView() { mListView = (ListView) findViewById(R.id.listView); } }5.activity_main:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.commonadapter.MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </android.support.constraint.ConstraintLayout>6.item:
<?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="horizontal"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> <ImageView android:id="@+id/image" android:layout_width="30dp" android:layout_height="20dp" /> </LinearLayout>这就封装好了。