Android快速开发之appBase——(2).万能的Adapter
android的Adapter是常用的一个组件,自定义的adapter基本上都是集成BaseAdapter,然后实现getView等一系列方法。时间长了,难免让人感觉到写的重复性代码过多,那么万能的Adapter讲解放你的双手。
对比
- BaseAdapter
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;
}
}
- AvAdapter(万能Adapter)
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();
}
}
- 结论:不用再写getView方法以及各种findviewbyid、getcout等方法。只需要掌握holder和data的配置规则就可以轻松使用。
Activity中使用
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);
}
}
- 被注释的是BaseAdapter
说明
- ListDemoAdapter是由代码生成器生成:包括layout的xml文件、Holder和Data的主体
- avlib框架基于注解配置,简化代码,让开发者更加关注其他业务的处理,UI上的处理将会越来越简单清晰。
最后
欢迎大家指点,有好的想法可以提出,我将会不断优化框架。
时间: 2024-10-07 07:02:14