ExpandableListView的美化以及出现的问题

如果有用到列表,有需要对列表分组,一般都会用到自定义ExpandableListView
,这个控件其实很自由的,可以与很多控件组合使用,现在实现以下的效果:


组与组分开可以用图片处理,图片关键是组与列表的拼接。然后就是自定义BaseExpandableListAdapter中加些判断,就好了,下面是代码:

主页布局main.xml




    
    


group布局group_item.xml




    

    


child布局child_item.xml




    

    


主页activity的类MainDemoActivity.java

package cn.zoeice.expandable;

import android.app.Activity;
import android.os.Bundle;

/**
 * ExpandableListView UI design
* Title: MainDemoActivity
* Description:
* Company: iceTree
* @author    zoeice
* @date       2012-7-9
 */
public class MainDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


主要是自定义MyExplandableListView的类MyExplandableListView.java

package cn.zoeice.expandable;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 自定义ExplandableListView
* Title: MyExplandableListView
* Description:
* Company: iceTree
* @author    zoeice
* @date       2012-7-9
 */
public class MyExplandableListView extends ExpandableListView 
			implements OnChildClickListener,OnGroupClickListener{
	
	ExpandInfoAdapter adapter;
	private Context context_;
	
	String[] str_group_items_ = {"第一组", "第二组", "第三组", "第四组", "第五组"};
	String[][] str_child_items_ = {{"这是第一条", "这是第二条"}, {"这是第一条", "这是第二条", "这是第三条"}, {"这是第一条"}, {"这是第一条", "这是第二条"}, {"这是第一条", "这是第二条", "这是第三条", "这是第四条"}};
	
	public MyExplandableListView(Context context) {
		super(context);
		context_ = context;
		/* 隐藏默认箭头显示 */
		this.setGroupIndicator(null);
		/* 隐藏垂直滚动条 */
		this.setVerticalScrollBarEnabled(false);
		
		/* 监听child,group点击事件 */
		this.setOnChildClickListener(this);
		this.setOnGroupClickListener(this);
		
		setCacheColorHint(Color.TRANSPARENT);
		setDividerHeight(0);
		setChildrenDrawnWithCacheEnabled(false);
		setGroupIndicator(null);
		
		/*隐藏选择的黄色高亮*/
		ColorDrawable drawable_tranparent_ = new ColorDrawable(Color.TRANSPARENT);
		setSelector(drawable_tranparent_);
		
		/*设置adapter*/
		adapter = new ExpandInfoAdapter();
		setAdapter(adapter);
	}
	
	
    
	public MyExplandableListView(Context context, AttributeSet attrs) {
		this(context);
		// TODO Auto-generated constructor stub
	}



	public class ExpandInfoAdapter extends BaseExpandableListAdapter {
	    	LinearLayout mGroupLayout;
    	
    	//++++++++++++++++++++++++++++++++++++++++++++
    	// child's stub
    	
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return str_child_items_[groupPosition][childPosition];
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}
		
		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return str_child_items_[groupPosition].length;
		}
		
		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView txt_child;
			if(null == convertView){
				convertView = LayoutInflater.from(context_).inflate(R.layout.child_item, null);  
			}
			/*判断是否是最后一项,最后一项设计特殊的背景*/
			if(isLastChild){
				convertView.setBackgroundResource(R.drawable.child_end);
			} else {
				convertView.setBackgroundResource(R.drawable.child);
			}
			
			txt_child = (TextView)convertView.findViewById(R.id.id_child_txt);
			txt_child.setText(str_child_items_[groupPosition][childPosition]);

			return convertView;
		}
				
		//++++++++++++++++++++++++++++++++++++++++++++
    	// group's stub
		
		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return str_group_items_[groupPosition];
		}

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return str_group_items_.length;
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,  View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView txt_group;
			if(null == convertView){
				convertView = LayoutInflater.from(context_).inflate(R.layout.group_item, null);  
			}
			/*判断是否group张开,来分别设置背景图*/
			if(isExpanded){
				convertView.setBackgroundResource(R.drawable.group_e);
			}else{
				convertView.setBackgroundResource(R.drawable.group);
			}
			
			txt_group = (TextView)convertView.findViewById(R.id.id_group_txt);
			txt_group.setText(str_group_items_[groupPosition]);
			return convertView;
		}
		
	        @Override
		public boolean isChildSelectable(int arg0, int arg1) {
			// TODO Auto-generated method stub
			return true;
		}
		
		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

	}
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		Toast.makeText(getContext(), "hi,你竟然点击了第" + (groupPosition + 1) + "组的第" + (childPosition + 1) + "条!", 2000).show();
		return false;
	}
	
	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		// TODO Auto-generated method stub
		return false;
	}

}

getView中如果用到ViewHolder的话不知道为什么会出现view缓存异常,可以帮我看一下嘿嘿

上代码:

http://download.csdn.net/detail/zoeice/4419170

你可能感兴趣的:(Android控件)