模拟QQ分组

时间:2021-11-27 10:12:43
 package com.lixu.fenzu;

 import java.util.ArrayList;
import java.util.HashMap; import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends ListActivity {
private static final String TYPE = "type";
private static final String DATE = "date";
private static final int GROUP = -1;
private static final int ITEM = -2; private ArrayList<HashMap<String, Object>> item = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); ListView lv = this.getListView();
ArrayAdapter adapter = new MyAdaper(this, 1); item = new ArrayList<HashMap<String, Object>>(); String[] groups = { "家人", "朋友", "同学", "同事" };
String[] date = { "张三", "李四", "王五", "狗儿" };
for (String str:groups) {
HashMap<String, Object> group_map = new HashMap<String, Object>(); group_map.put(TYPE, GROUP);
group_map.put(DATE, str);
item.add(group_map);
for (String n : date) { HashMap<String, Object> date_map = new HashMap<String, Object>(); date_map.put(TYPE, ITEM);
date_map.put(DATE, n);
item.add(date_map); } }
lv.setAdapter(adapter); } public class MyAdaper extends ArrayAdapter {
private LayoutInflater flater = null; public MyAdaper(Context context, int resource) {
super(context, resource);
flater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} @Override
public int getCount() {
return item.size();
} @Override
public int getItemViewType(int position) {
HashMap<String, Object> map = item.get(position);
return (Integer) map.get(TYPE);
} @Override
public int getViewTypeCount() { return 2;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
// 根据不同的view type加载不同的布局文件。
switch (type) {
case GROUP: convertView = flater.inflate(R.layout.activity_main, null);
TextView tv1 = (TextView) convertView.findViewById(R.id.tv1);
tv1.setText(item.get(position).get(DATE) + "");
tv1.setBackgroundColor(Color.RED);
tv1.setTextSize(20); break;
case ITEM:
convertView = flater.inflate(R.layout.activity_main, null);
TextView tv2 = (TextView) convertView.findViewById(R.id.tv1);
tv2.setText(item.get(position).get(DATE) + ""); break; default:
break;
} return convertView;
} }
}

运行效果:

模拟QQ分组