关闭

Adapter实现多种布局

171人阅读 评论(0) 收藏 举报

该方法实现的分组无法打开或者关闭

效果图:

java代码

package com.xiaoke.Manyadapter;

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 final String DATA = "data";  
    private final String TYPE = "type";  
  
    private final int GROUP = -2;  
    private final int ITEM = -3; 
    private ArrayList<HashMap<String, Object>> list = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
		ListView listView=this.getListView();
		list=new ArrayList<HashMap<String,Object>>();
		
		ArrayAdapter myAdapter=new PangAdapter(this,-1);
		listView.setAdapter(myAdapter);
		String[] groups={"A","B","C","D","E"};
		int count = 0;
		for(int i=0;i<groups.length;i++){
			HashMap<String, Object> group_map = new HashMap<String, Object>();  
            group_map.put(TYPE, GROUP);  
            group_map.put(DATA, groups[i]);  
            list.add(group_map); 
			for(int j=0;j<5;j++){
				HashMap<String, Object> data_map = new HashMap<String, Object>();  
                data_map.put(TYPE, ITEM);  
                data_map.put(DATA, "数据:" + (count++));  
                list.add(data_map);
			}
			
		}
		
		
	}
	
	public class PangAdapter extends ArrayAdapter{
		LayoutInflater inflater=null;
		public PangAdapter(Context context, int resource) {
			super(context, resource);
			Log.d("执行顺序", "1");
			inflater=LayoutInflater.from(context);
			//flater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		//返回长度
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			Log.d("执行顺序", "2");
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			Log.d("执行顺序", "3");
			HashMap<String, Object> map = list.get(position);  
            return map.get(DATA);  
		}		

		@Override
		public int getItemViewType(int position) {
			Log.d("执行顺序", "4");
			HashMap<String, Object> map = list.get(position);  
            return (Integer) map.get(TYPE);
		}
		//返回显示的类型个数
		@Override
		public int getViewTypeCount() {
			Log.d("执行顺序", "5");
			// TODO Auto-generated method stub
			return 2;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.d("执行顺序", "6");
			int type = getItemViewType(position); 
			switch (type) {  
            case GROUP:  
                convertView = inflater.inflate(  
                        android.R.layout.simple_list_item_2, null);  
                TextView tv1 = (TextView) convertView  
                        .findViewById(android.R.id.text1);  
                tv1.setText("分组");  
                tv1.setBackgroundColor(Color.RED);  
                TextView tv2 = (TextView) convertView  
                        .findViewById(android.R.id.text2);  
                tv2.setText(getItem(position) + "");  
                tv2.setBackgroundColor(Color.GRAY);  
                break;  
            case ITEM:  
                convertView = inflater.inflate(  
                        android.R.layout.simple_list_item_1, null);  
                TextView tv = (TextView) convertView  
                        .findViewById(android.R.id.text1);  
                tv.setText(getItem(position) + "");  
                break;  
            }  
  
            return convertView;  
		}
		
		
		
	}
}
xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
该代码大部分都出自下面的博客,如需请访问

http://blog.csdn.net/zhangphil

点击打开链接

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10958次
    • 积分:568
    • 等级:
    • 排名:千里之外
    • 原创:47篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条
    文章分类