在上一篇中,我們說過,在setData中如果有很多控件的話,我們還是要在該方法中寫入很多代碼,為了降低開發(fā)的方便性,本次就在此基礎(chǔ)上再一次優(yōu)化。實現(xiàn)原理是這樣的,每次在setData中都要查找控件,然后setXXX()什么的,我們可以把這寫實現(xiàn)放如到ViewHolder中去,在ViewHolder中寫入一個鏈?zhǔn)降姆椒?,來幫助我們來實現(xiàn)功能(關(guān)于ViewHodler類的代碼我就不再重復(fù)寫了,代碼在此:Android之自定義實現(xiàn)BaseAdapter(通用適配器一) ),鏈?zhǔn)椒椒ㄈ缦拢?/p>
成都網(wǎng)站建設(shè)、做網(wǎng)站介紹好的網(wǎng)站是理念、設(shè)計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計理念、多方位的設(shè)計風(fēng)格、經(jīng)驗豐富的設(shè)計團隊。提供PC端+手機端網(wǎng)站建設(shè),用營銷思維進行網(wǎng)站設(shè)計、采用先進技術(shù)開源代碼、注重用戶體驗與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。
public ViewHolder setText(int viewId, String data){ TextView tv = getView(viewId); tv.setText(data); return this; }
通過這個方法,我們可以很好的去設(shè)置對應(yīng)的內(nèi)容,只需要傳一個需要設(shè)置控件的id,然后傳對應(yīng)的數(shù)據(jù),就可以達(dá)到設(shè)置文字的效果了,下面再看看我們的MyAdapter.java中的代碼
MyAdapter.java
/** * 上一篇中的MyAdapter */ public class MyAdapter extends MyBaseAdapter { public MyAdapter(Listdata) { 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()); } } /** * 優(yōu)化后的MyAdapter */ public class MyAdapter extends MyBaseAdapter { public MyAdapter(List 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中的實現(xiàn),我們這里只用一條代碼就可以代替上面的實現(xiàn)了,這樣是不是更方便呢,這樣的話,我們的擴展就更加方便了,如果說,我們不僅僅只是設(shè)置文本內(nèi)容,而是通過圖片控件設(shè)置圖片呢?很簡單,我們只需要在ViewHolder中再添加我們要實現(xiàn)的方法即可,比如設(shè)置圖片,我們可以添加如下代碼:
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; }
添加這兩個方法后,需要設(shè)置圖片就很輕松了,只需在setData中傳遞對應(yīng)的參數(shù)即可
@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); }
好了,一個通用的適配器就已經(jīng)完成了,整體框架和代碼都已經(jīng)實現(xiàn)了,我將所有的代碼都綜合一下,寫在下面,方便大家使用,分別是MainActivity.java(主界面類,負(fù)責(zé)傳參設(shè)置ListView的數(shù)據(jù))、MyAdapter.java(自定義的適配器)、MyBaseAdapter.java(通用的適配器類)、ViewHolder.java(通用的持有類對象)、以及實體類Student.java
MainActivity.java
public class MainActivity extends AppCompatActivity{ private Listdata; 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{ public MyAdapter(List 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 MyBaseAdapterextends BaseAdapter { protected List data; public MyBaseAdapter(List 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 SparseArrayarray; 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 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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。