因为一般Adapter只提供文字显示,比如SimpleAdapter,就只是传递一个数据对象、一个布局还有一个数据和布局的对应关系,最终你只能得到文本的变化,如果你需要图片,还根据数据而变化的,那就需要自定义了。
例子程序:
//code by hqlulu http://www.aslibra.com/
package com.aslibra.reader;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AdapterCategoryList extends BaseAdapter {
private LayoutInflater mInflater;
private List<Map<String,String>> item;
private Context mycontext;
public AdapterCategoryList(Context context, List<Map<String,String>> items){
mycontext = context;
mInflater = LayoutInflater.from(context);
item = items;
}
@Override
public int getCount() {
//返回对象大小,会根据这个来遍历处理界面的
return item.size();
}
@Override
public Map<String,String> getItem(int position) {
//返回相应位置的数据
return item.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.list_row, null);
}
TextView tv;
ImageView iv;
//文字定义
tv = (TextView) convertView.findViewById(R.id.text_2);
tv.setText(this.getItem(position).get("text"));
tv = (TextView) convertView.findViewById(R.id.text_3);
tv.setText(this.getItem(position).get("icon"));
//图片定义例子
iv = (ImageView) convertView.findViewById(R.id.category_icon);
if(position>3)
iv.setImageDrawable(mycontext.getResources().getDrawable(R.drawable.mag_icon));
else
iv.setImageDrawable(mycontext.getResources().getDrawable(R.drawable.icon_mag));
return convertView;
}
}
package com.aslibra.reader;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AdapterCategoryList extends BaseAdapter {
private LayoutInflater mInflater;
private List<Map<String,String>> item;
private Context mycontext;
public AdapterCategoryList(Context context, List<Map<String,String>> items){
mycontext = context;
mInflater = LayoutInflater.from(context);
item = items;
}
@Override
public int getCount() {
//返回对象大小,会根据这个来遍历处理界面的
return item.size();
}
@Override
public Map<String,String> getItem(int position) {
//返回相应位置的数据
return item.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.list_row, null);
}
TextView tv;
ImageView iv;
//文字定义
tv = (TextView) convertView.findViewById(R.id.text_2);
tv.setText(this.getItem(position).get("text"));
tv = (TextView) convertView.findViewById(R.id.text_3);
tv.setText(this.getItem(position).get("icon"));
//图片定义例子
iv = (ImageView) convertView.findViewById(R.id.category_icon);
if(position>3)
iv.setImageDrawable(mycontext.getResources().getDrawable(R.drawable.mag_icon));
else
iv.setImageDrawable(mycontext.getResources().getDrawable(R.drawable.icon_mag));
return convertView;
}
}
这几个方法是基本要重写的,最关键的是getView,这里是处理每行的界面的,要标新立异就在这里面了。
值得一提的是,当界面发生变化时,会重新调用getView,拖动时会发生,以便重绘界面。
显示的处理:
//code by hqlulu http://www.aslibra.com/
//List<Map<String,String>> items = xxxx(取数据)
AdapterCategoryList mListAdapter = new AdapterCategoryList(this, items);
ListView myListView = (ListView) findViewById("aslibra_com");
myListView.setAdapter(mListAdapter);
//List<Map<String,String>> items = xxxx(取数据)
AdapterCategoryList mListAdapter = new AdapterCategoryList(this, items);
ListView myListView = (ListView) findViewById("aslibra_com");
myListView.setAdapter(mListAdapter);