目前我 listView 用的还是比较多,所以经常要适配器。就发现很多重复的步骤了。赶在今天状态好,就试着写了一个IBaseAdapter
因为泛型不会用 百度了才知道怎么用。然后才知道 这个Baseadapter大神们早就写了,跟我想的没差。哦,应该是我的跟大神的基本相同。就记录一下了
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;
}
}
继承 该基类,实现两个方法
getLayoutId()返回 布局ID
getView(int position, View convertView) 里面操作你要写的逻辑
在 getView(int position, View convertView) 方法里面可以使用 getItem(int position)得到实体模型数据
然后提供一些七七八八的便捷方法
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);
}
}
大概就这样了。下班收队