您好,登錄后才能下訂單哦!
在上一篇中,我們說過,在setData中如果有很多控件的話,我們還是要在該方法中寫入很多代碼,為了降低開發的方便性,本次就在此基礎上再一次優化。實現原理是這樣的,每次在setData中都要查找控件,然后setXXX()什么的,我們可以把這寫實現放如到ViewHolder中去,在ViewHolder中寫入一個鏈式的方法,來幫助我們來實現功能(關于ViewHodler類的代碼我就不再重復寫了,代碼在此:Android之自定義實現BaseAdapter(通用適配器一) ),鏈式方法如下:
public ViewHolder setText(int viewId, String data){ TextView tv = getView(viewId); tv.setText(data); return this; }
通過這個方法,我們可以很好的去設置對應的內容,只需要傳一個需要設置控件的id,然后傳對應的數據,就可以達到設置文字的效果了,下面再看看我們的MyAdapter.java中的代碼
MyAdapter.java
/** * 上一篇中的MyAdapter */ public class MyAdapter extends MyBaseAdapter { public MyAdapter(List<Student> data) { super(data); } @Override public void setData(ViewHolder holder, Student t) { TextView tvName = holder.getView(R.id.mTv1); tvName.setText(t.getName()); TextView tvSex = holder.getView(R.id.mTv2); tvSex.setText(t.getSex()); } } /** * 優化后的MyAdapter */ public class MyAdapter extends MyBaseAdapter { public MyAdapter(List<Student> data) { super(data); } @Override public void setData(ViewHolder holder, Student t) { holder.setText(R.id.mTv1, t.getName()).setText(R.id.mTv2, t.getSex()); } }
ok,對比一下setData中的實現,我們這里只用一條代碼就可以代替上面的實現了,這樣是不是更方便呢,這樣的話,我們的擴展就更加方便了,如果說,我們不僅僅只是設置文本內容,而是通過圖片控件設置圖片呢?很簡單,我們只需要在ViewHolder中再添加我們要實現的方法即可,比如設置圖片,我們可以添加如下代碼:
public ViewHolder setImageResource(int viewId,int resId){ ImageView img = getView(viewId); img.setImageResource(resId); return this; } public ViewHolder setImageBitmap(int viewId, Bitmap bm){ ImageView img = getView(viewId); img.setImageBitmap(bm); return this; }
添加這兩個方法后,需要設置圖片就很輕松了,只需在setData中傳遞對應的參數即可
@Override public void setData(ViewHolder holder, Student t) { holder.setText(R.id.mTv1, t.getName()).setText(R.id.mTv2, t.getSex()); holder.setImageResource(R.id.img1,資源id).setImageBitmap(R.id.img2,bm); }
好了,一個通用的適配器就已經完成了,整體框架和代碼都已經實現了,我將所有的代碼都綜合一下,寫在下面,方便大家使用,分別是MainActivity.java(主界面類,負責傳參設置ListView的數據)、MyAdapter.java(自定義的適配器)、MyBaseAdapter.java(通用的適配器類)、ViewHolder.java(通用的持有類對象)、以及實體類Student.java
MainActivity.java
public class MainActivity extends AppCompatActivity{ private List<Student> data; private ListView mList; MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getData(); mList = (ListView) findViewById(R.id.mList); adapter = new MyAdapter(data); mList.setAdapter(adapter); } private void getData() { data = new ArrayList<>(); Student stu = null; for (int i = 0; i < 20; i++) { stu = new Student(); stu.setName("姓名" + i); stu.setSex(i % 2 == 0 ? "男" : "女"); data.add(stu); } } }
MyAdapter.java
public class MyAdapter extends MyBaseAdapter<Student> { public MyAdapter(List<Student> data) { super(data); } @Override public void setData(ViewHolder holder, Student t) { holder.setText(R.id.mTv1, t.getName()).setText(R.id.mTv2, t.getSex()); } }
MyBaseAdapter.java
public abstract class MyBaseAdapter<T> extends BaseAdapter { protected List<T> data; public MyBaseAdapter(List<T> data){ this.data = data; } @Override public int getCount() { return data == null ? 0 : data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = ViewHolder.getHolder(convertView,parent,position, R.layout.list_item); setData(holder,data.get(position)); return holder.getConvertView(); } public abstract void setData(ViewHolder holder,T t); }
ViewHolder.java
public class ViewHolder { private int position; private SparseArray<View> array; private View convertView; private Context context; private ViewHolder(ViewGroup parent, int position, int layout) { this.position = position; this.context = parent.getContext(); convertView = LayoutInflater.from(parent.getContext()).inflate(layout, null); convertView.setTag(this); array = new SparseArray<>(); } public static ViewHolder getHolder(View convertView, ViewGroup parent, int position, int layout) { if (convertView == null) { return new ViewHolder(parent, position, layout); } else { ViewHolder holder = (ViewHolder) convertView.getTag(); holder.position = position; return holder; } } public <T extends View> T getView(int viewId) { View view = array.get(viewId); if (view == null) { view = convertView.findViewById(viewId); array.put(viewId, view); } return (T) view; } public View getConvertView() { return convertView; } public ViewHolder setText(int viewId, String data) { TextView tv = getView(viewId); tv.setText(data); return this; } public ViewHolder setImageResource(int viewId, int resId) { ImageView img = getView(viewId); img.setImageResource(resId); return this; } public ViewHolder setImageBitmap(int viewId, Bitmap bm) { ImageView img = getView(viewId); img.setImageBitmap(bm); return this; } }
Student.java
public class Student { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。