android的Adapter是常用的一个组件,自定义的adapter基本上都是集成BaseAdapter,然后实现getView等一系列方法。时间长了,难免让人感觉到写的重复性代码过多,那么万能的Adapter讲解放你的双手。
package com.snicesoft.appbase.demo;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListBaseAdapter extends BaseAdapter {
List<Data> datas = new ArrayList<ListBaseAdapter.Data>();
public void setDatas(List<Data> datas) {
this.datas = datas;
notifyDataSetChanged();
}
public class Holder {
TextView tvTitle;
ImageView imgIcon;
}
public static class Data {
String title;
int icon;
public Data(String title, int icon) {
super();
this.title = title;
this.icon = icon;
}
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView == null){
holder = new Holder();
convertView = View.inflate(parent.getContext(), R.layout.item_listdemo, null);
holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
holder.imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
holder.tvTitle.setText(datas.get(position).title);
holder.imgIcon.setImageResource(datas.get(position).icon);
return convertView;
}
}
package com.snicesoft.appbase.demo;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
import com.snicesoft.avlib.annotation.DataBind;
import com.snicesoft.avlib.annotation.DataType;
import com.snicesoft.avlib.annotation.Id;
import com.snicesoft.avlib.annotation.Layout;
import com.snicesoft.avlib.rule.IData;
import com.snicesoft.avlib.rule.IHolder;
import com.snicesoft.avlib.widget.AvAdapter;
@Layout(R.layout.item_listdemo)
public class ListDemoAdapter extends
AvAdapter<ListDemoAdapter.Holder, ListDemoAdapter.Data> {
public class Holder extends IHolder {
@Id(R.id.tvTitle)
TextView tvTitle;
@Id(R.id.imgIcon)
ImageView imgIcon;
@Override
public void initViewParams() {
}
}
public static class Data extends IData {
@DataBind(id = R.id.tvTitle)
String title;
@DataBind(id = R.id.imgIcon, dataType = DataType.IMG)
int icon;
public Data(String title, int icon) {
super();
this.title = title;
this.icon = icon;
}
}
public ListDemoAdapter(Context context) {
super(context);
}
@Override
public Holder newHolder() {
return new Holder();
}
}
package com.snicesoft.appbase.demo;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.snicesoft.avlib.annotation.DataBind;
import com.snicesoft.avlib.annotation.DataType;
import com.snicesoft.avlib.annotation.Id;
import com.snicesoft.avlib.annotation.Layout;
import com.snicesoft.avlib.rule.IData;
import com.snicesoft.avlib.rule.IHolder;
import com.snicesoft.base.BaseActivity;
@Layout(R.layout.activity_main)
public class MainActivity extends
BaseActivity<MainActivity.Holder, MainActivity.Data> {
public class Holder extends IHolder {
@Id(R.id.textView1)
TextView textView1;
@Id(R.id.button1)
Button button1;
@Id(R.id.listView1)
ListView listView1;
@Override
public void initViewParams() {
}
}
public class Data extends IData {
@DataBind(id = R.id.textView1)
String tv1 = "我是自动绑定的TextView";
@DataBind(id = R.id.button1)
String btn1 = "我是自动绑定的Button";
@DataBind(id = R.id.listView1, dataType = DataType.ADAPTER)
// ListBaseAdapter adapter = new ListBaseAdapter();
ListDemoAdapter adapter = new ListDemoAdapter(MainActivity.this);
}
@Override
public Data newData() {
return new Data();
}
@Override
public Holder newHolder() {
return new Holder();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// List<ListBaseAdapter.Data> datas = new ArrayList<ListBaseAdapter.Data>();
List<ListDemoAdapter.Data> datas = new ArrayList<ListDemoAdapter.Data>();
for (int i = 1; i <= 10; i++) {
// datas.add(new ListBaseAdapter.Data("标题"+i, R.drawable.ic_launcher));
datas.add(new ListDemoAdapter.Data("标题"+i, R.drawable.ic_launcher));
}
_data.adapter.addAll(datas);
}
}
欢迎大家指点,有好的想法可以提出,我将会不断优化框架。
Android快速开发之appBase——(2).万能的Adapter
原文地址:http://blog.csdn.net/jflex/article/details/46432127