您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Android如何自定義實現(xiàn)BaseAdapter的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
實現(xiàn)原理是這樣的,每次在setData中都要查找控件,然后setXXX()什么的,我們可以把這寫實現(xiàn)放如到ViewHolder中去,在ViewHolder中寫入一個鏈式的方法,來幫助我們來實現(xiàn)功能,鏈式方法如下:
public ViewHolder setText(int viewId, String data){ TextView tv = getView(viewId); tv.setText(data); return this; }
通過這個方法,我們可以很好的去設(shè)置對應的內(nèi)容,只需要傳一個需要設(shè)置控件的id,然后傳對應的數(shù)據(jù),就可以達到設(shè)置文字的效果了,下面再看看我們的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()); }}/*** 優(yōu)化后的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中的實現(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中傳遞對應的參數(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(主界面類,負責傳參設(shè)置ListView的數(shù)據(jù))、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; }}
感謝各位的閱讀!關(guān)于“Android如何自定義實現(xiàn)BaseAdapter”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。