您好,登錄后才能下訂單哦!
目前我 listView 用的還是比較多,所以經(jīng)常要適配器。就發(fā)現(xiàn)很多重復(fù)的步驟了。趕在今天狀態(tài)好,就試著寫了一個(gè)IBaseAdapter
因?yàn)榉盒筒粫?huì)用 百度了才知道怎么用。然后才知道 這個(gè)Baseadapter大神們?cè)缇蛯懥耍蚁氲臎]差。哦,應(yīng)該是我的跟大神的基本相同。就記錄一下了 IBaseAdapter<> BaseAdapter { Context getContext() { ; } Context ; List<> ; List<> getmLists() { ; } setmLists(List<> mLists) { .= mLists; } IBaseAdapter(Context context,List<> mLists) { init(context,mLists); } IBaseAdapter(Context context) { init(context,ArrayList<>()); } upDate(List<> mLists){ .= mLists; notifyDataSetChanged(); } clean(){ (!= ) { ..clear(); notifyDataSetChanged(); } } addAll(List<> list) { (!= list) { ..addAll(list); notifyDataSetChanged(); } } remove(position){ (!= ) { ..remove(position); notifyDataSetChanged(); } } init(Context context,List<> mLists){ .= context; .= mLists; } getLayoutId(); getView(position, View convertView); getCount() { == ?:.size(); } getItem(position) { == ?:.get(position); } getItemId(position) { position; } View getView(position, View convertView, ViewGroup parent) { (convertView == ){ layoutId = getLayoutId(); (layoutId == ){ NullPointerException(); } convertView = LayoutInflater.().inflate(layoutId,parent,); AutoUtils.(convertView); } getView(position,convertView); convertView; } () <View> (View convertView, id) { SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag(); (viewHolder == ) { viewHolder = SparseArray<View>(); convertView.setTag(viewHolder); } View childView = viewHolder.get(id); (childView == ) { childView = convertView.findViewById(id); viewHolder.put(id, childView); } () childView; } }
繼承 該基類,實(shí)現(xiàn)兩個(gè)方法
getLayoutId()返回 布局ID getView(int position, View convertView) 里面操作你要寫的邏輯 在 getView(int position, View convertView) 方法里面可以使用 getItem(int position)得到實(shí)體模型數(shù)據(jù) 然后提供一些七七八八的便捷方法 findChildView()是省去寫ViewHolder的方法 附上使用例子 /** * Created by chenguozhen on 2017/3/30. * eMail 1021632321@QQ.com */ public class MainAuthorAdapter extends IBaseAdapter<RecommentAuthor> { public MainAuthorAdapter(Context context, List<RecommentAuthor> mLists) { super(context, mLists); } @Nullable @Override public int getLayoutId(){ return R.layout.list_column_item; } @Override public void getView(int position, View convertView) { RecommentAuthor recommentAuthor = getItem(position); ImageView head = findChildView(convertView, R.id.ivHeadForListColumn); TextView title = findChildView(convertView, R.id.tvTitleForListColumn); TextView content = findChildView(convertView, R.id.tvContextForListColumn); TextView time = findChildView(convertView, R.id.tvTimeForListColumn); TextView money = findChildView(convertView, R.id.tvMoneyForListColumn); GlideManager.loadImage(getContext(), recommentAuthor.getHead_img(), R.mipmap.default_head_ic, R.mipmap.default_head_ic, head); title.setText(recommentAuthor.getAuthor_name()); content.setText(recommentAuthor.getColumn_name()); time.setText(recommentAuthor.getCreate_date()); money.setVisibility(View.GONE); } }
大概就這樣了。下班收隊(duì)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。