欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android实现listview tag,重写adapter 博客分类: Android

程序员文章站 2024-03-02 11:30:10
...
public class SoftListAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<Map<String, Object>> list;

	public SoftListAdapter(Context con, ArrayList<Map<String, Object>> lis) {
		this.context = con;
		this.list = lis;
	}

	// 决定 getView循环几次
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	// 自定义适配方法
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (list.get(position).get("tag") != null
				&& (Boolean) list.get(position).get("tag")) {
			// 如果是标签项
			convertView = LayoutInflater.from(context.getApplicationContext())
					.inflate(R.layout.permission_soft_vlist_tag, null);
		} else {
			// 否则就是数据项
			convertView = LayoutInflater.from(context.getApplicationContext())
					.inflate(R.layout.permission_soft_vlist, null);
			ImageView img = (ImageView) convertView.findViewById(R.id.img);
			img.setImageDrawable((Drawable) list.get(position).get("img"));
		}

		TextView text = (TextView) convertView.findViewById(R.id.text);
		text.setText(list.get(position).get("text").toString());

		return convertView;
	}
}

	ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			map.put("text", tagString);
			map.put("tag", true);
			data.add(map);

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="5dip" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:src="@drawable/ic_launcher"
        android:textColor="#FFFFFFFF"
        android:textSize="22dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:textColor="#FFFFFFFF"
        android:textSize="18dp" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_color"
    android:paddingBottom="5dip"
    android:paddingLeft="10dip"
    android:paddingTop="5dip" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="20dip"
        android:gravity="center_vertical"
        android:textColor="#ffffff" >
    </TextView>

</LinearLayout>