這篇文章給大家分享的是有關(guān)Android如何自定義實(shí)現(xiàn)BaseAdapter的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
在貴定等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站制作 網(wǎng)站設(shè)計制作按需開發(fā)網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站,外貿(mào)營銷網(wǎng)站建設(shè),貴定網(wǎng)站建設(shè)費(fèi)用合理。
實(shí)現(xiàn)原理是這樣的,每次在setData中都要查找控件,然后setXXX()什么的,我們可以把這寫實(shí)現(xiàn)放如到ViewHolder中去,在ViewHolder中寫入一個鏈?zhǔn)降姆椒?,來幫助我們來?shí)現(xiàn)功能,鏈?zhǔn)椒椒ㄈ缦拢?/p>
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(List 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 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中的實(shí)現(xiàn),我們這里只用一條代碼就可以代替上面的實(shí)現(xiàn)了,這樣是不是更方便呢,這樣的話,我們的擴(kuò)展就更加方便了,如果說,我們不僅僅只是設(shè)置文本內(nèi)容,而是通過圖片控件設(shè)置圖片呢?很簡單,我們只需要在ViewHolder中再添加我們要實(shí)現(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)實(shí)現(xiàn)了,我將所有的代碼都綜合一下,寫在下面,方便大家使用,分別是MainActivity.java(主界面類,負(fù)責(zé)傳參設(shè)置ListView的數(shù)據(jù))、MyAdapter.java(自定義的適配器)、MyBaseAdapter.java(通用的適配器類)、ViewHolder.java(通用的持有類對象)、以及實(shí)體類Student.java
MainActivity.java
public class MainActivity extends AppCompatActivity{ private List 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 { 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 MyBaseAdapter extends 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 SparseArray 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 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如何自定義實(shí)現(xiàn)BaseAdapter”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
本文標(biāo)題:Android如何自定義實(shí)現(xiàn)BaseAdapter
文章位置:
http://weahome.cn/article/jsgdpd.html