ExpandableListView小图标替换

ExpandableListView的小图标有个状态,一个是不点击的情况,一个是点击后展开的情况,用xml配置如下:
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.        <item android:state_expanded="true" android:drawable="@drawable/narrow_select" />  
  4.        <item android:drawable="@drawable/narrow" />  
  5. </selector>  

Java的代码如下:
Java代码 复制代码  收藏代码
  1. ExpandableListView listView = getExpandableListView();   
  2. listView.setGroupIndicator(this.getResources().getDrawable(R.drawable.group_icon_selector));  
ExpandableListView listView = getExpandableListView();
listView.setGroupIndicator(this.getResources().getDrawable(R.drawable.group_icon_selector));

注意:不知道为什么,使用自定义的GroupIndicator会发生图片被拉伸的现象,为了解决这个问题需要定义一个长度为groups.length的boolean数组:
Java代码 复制代码  收藏代码
  1. private boolean[] isOpen=new boolean[groups.length];  
private boolean[] isOpen=new boolean[groups.length];

然后重写OnGroupCollapseListener和OnGroupExpandListener用于修改当前group的状态:
Java代码 复制代码  收藏代码
  1. expandableListView.setOnGroupCollapseListener(onGroupCollapseListener);   
  2. expandableListView.setOnGroupExpandListener(onGroupExpandListener);   
  3. expandableListView.setGroupIndicator(null);//不要自带的了!!!   
  4. OnGroupCollapseListener onGroupCollapseListener=new OnGroupCollapseListener(){   
  5.   
  6.             @Override  
  7.             public void onGroupCollapse(int groupPosition) {   
  8.                 // TODO Auto-generated method stub   
  9.                 isOpen[groupPosition]=false;   
  10.             }   
  11.                
  12.         };   
  13.            
  14. OnGroupExpandListener onGroupExpandListener=new OnGroupExpandListener(){   
  15.   
  16.             @Override  
  17.             public void onGroupExpand(int groupPosition) {   
  18.                 // TODO Auto-generated method stub   
  19.                 isOpen[groupPosition]=true;   
  20.             }   
  21.                
  22.         };  
expandableListView.setOnGroupCollapseListener(onGroupCollapseListener);
expandableListView.setOnGroupExpandListener(onGroupExpandListener);
expandableListView.setGroupIndicator(null);//不要自带的了!!!
OnGroupCollapseListener onGroupCollapseListener=new OnGroupCollapseListener(){

			@Override
			public void onGroupCollapse(int groupPosition) {
				// TODO Auto-generated method stub
				isOpen[groupPosition]=false;
			}
			
		};
		
OnGroupExpandListener onGroupExpandListener=new OnGroupExpandListener(){

			@Override
			public void onGroupExpand(int groupPosition) {
				// TODO Auto-generated method stub
				isOpen[groupPosition]=true;
			}
			
		};

最后在getGroupView()中应用,代码片段如下:
Java代码 复制代码  收藏代码
  1. @Override  
  2. public View getGroupView(int groupPosition, boolean isExpanded,   
  3.                     View convertView, ViewGroup parent) {   
  4.                 // TODO Auto-generated method stub   
  5.                 TextView textView=null;   
  6.                 if(convertView==null){   
  7.                     textView=new TextView(mContext);   
  8.                     AbsListView.LayoutParams lp = new AbsListView.LayoutParams(     
  9.                             ViewGroup.LayoutParams.FILL_PARENT, 50);     
  10.                     textView.setLayoutParams(lp);   
  11.                     textView.setGravity(Gravity.CENTER);   
  12.                     textView.setPadding(10000);   
  13.                     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.0f);   
  14.                     textView.setTextColor(0xFFC6B39C);   
  15.                     textView.setBackgroundResource(R.drawable.bg_tv);//bg_tv_expand   
  16.                     convertView=textView;   
  17.                 }else{   
  18.                     textView=(TextView)convertView;   
  19.                 }   
  20.                 //为了处理图标被拉伸的问题!这里采用偷懒的办法让textView傍边产生一个图标,如果GroupView只是一个TextView的话,推荐这样做!   
  21.                 if(isOpen[groupPosition]){   
  22.                     Drawable leftDrawable= resources.getDrawable(R.drawable.arrow_down_on);   
  23.                     leftDrawable.setBounds(00, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());   
  24.                     textView.setCompoundDrawables(leftDrawable, nullnullnull);   
  25.                 }else{   
  26.                     Drawable leftDrawable= resources.getDrawable(R.drawable.arrow_right_off);   
  27.                     leftDrawable.setBounds(00, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());   
  28.                     textView.setCompoundDrawables(leftDrawable, nullnullnull);   
  29.                 }   
  30.                 textView.setText(((Market)getGroup(groupPosition)).market);   
  31.                 return textView;   
  32. }  
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				TextView textView=null;
				if(convertView==null){
					textView=new TextView(mContext);
					AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
		                    ViewGroup.LayoutParams.FILL_PARENT, 50);  
		            textView.setLayoutParams(lp);
		            textView.setGravity(Gravity.CENTER);
		            textView.setPadding(10, 0, 0, 0);
		            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.0f);
		            textView.setTextColor(0xFFC6B39C);
		            textView.setBackgroundResource(R.drawable.bg_tv);//bg_tv_expand
		            convertView=textView;
				}else{
					textView=(TextView)convertView;
				}
				//为了处理图标被拉伸的问题!这里采用偷懒的办法让textView傍边产生一个图标,如果GroupView只是一个TextView的话,推荐这样做!
				if(isOpen[groupPosition]){
					Drawable leftDrawable= resources.getDrawable(R.drawable.arrow_down_on);
	            	leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
	            	textView.setCompoundDrawables(leftDrawable, null, null, null);
				}else{
					Drawable leftDrawable= resources.getDrawable(R.drawable.arrow_right_off);
					leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
					textView.setCompoundDrawables(leftDrawable, null, null, null);
				}
				textView.setText(((Market)getGroup(groupPosition)).market);
	            return textView;
}


over!

Android版手风琴(ExpandableListView)

创新源于模仿之四:增强的ExpandableListView

android api里的例子:

Java代码 复制代码  收藏代码
  1. import android.app.ExpandableListActivity;   
  2. import android.os.Bundle;   
  3. import android.view.ContextMenu;   
  4. import android.view.Gravity;   
  5. import android.view.MenuItem;   
  6. import android.view.View;   
  7. import android.view.ViewGroup;   
  8. import android.view.ContextMenu.ContextMenuInfo;   
  9. import android.widget.AbsListView;   
  10. import android.widget.BaseExpandableListAdapter;   
  11. import android.widget.ExpandableListAdapter;   
  12. import android.widget.ExpandableListView;   
  13. import android.widget.TextView;   
  14. import android.widget.Toast;   
  15. import android.widget.ExpandableListView.ExpandableListContextMenuInfo;   
  16.   
  17. import com.example.android.apis.R;   
  18.   
  19. /**  
  20.  * Demonstrates expandable lists using a custom {@link ExpandableListAdapter}  
  21.  * from {@link BaseExpandableListAdapter}.  
  22.  */  
  23. public class ExpandableList1 extends ExpandableListActivity {   
  24.   
  25.     ExpandableListAdapter mAdapter;   
  26.   
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState) {   
  29.         super.onCreate(savedInstanceState);   
  30.   
  31.         // Set up our adapter   
  32.         mAdapter = new MyExpandableListAdapter();   
  33.         setListAdapter(mAdapter);   
  34.         registerForContextMenu(getExpandableListView());   
  35.     }   
  36.   
  37.     @Override  
  38.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {   
  39.         menu.setHeaderTitle("Sample menu");   
  40.         menu.add(000, R.string.expandable_list_sample_action);   
  41.     }   
  42.        
  43.     @Override  
  44.     public boolean onContextItemSelected(MenuItem item) {   
  45.         ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();   
  46.   
  47.         String title = ((TextView) info.targetView).getText().toString();   
  48.            
  49.         int type = ExpandableListView.getPackedPositionType(info.packedPosition);   
  50.         if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {   
  51.             int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);    
  52.             int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);    
  53.             Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,   
  54.                     Toast.LENGTH_SHORT).show();   
  55.             return true;   
  56.         } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {   
  57.             int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);    
  58.             Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();   
  59.             return true;   
  60.         }   
  61.            
  62.         return false;   
  63.     }   
  64.   
  65.     /**  
  66.      * A simple adapter which maintains an ArrayList of photo resource Ids.   
  67.      * Each photo is displayed as an image. This adapter supports clearing the  
  68.      * list of photos and adding a new photo.  
  69.      *  
  70.      */  
  71.     public class MyExpandableListAdapter extends BaseExpandableListAdapter {   
  72.         // Sample data set.  children[i] contains the children (String[]) for groups[i].   
  73.         private String[] groups = { "People Names""Dog Names""Cat Names""Fish Names" };   
  74.         private String[][] children = {   
  75.                 { "Arnold""Barry""Chuck""David" },   
  76.                 { "Ace""Bandit""Cha-Cha""Deuce" },   
  77.                 { "Fluffy""Snuggles" },   
  78.                 { "Goldy""Bubbles" }   
  79.         };   
  80.            
  81.         public Object getChild(int groupPosition, int childPosition) {   
  82.             return children[groupPosition][childPosition];   
  83.         }   
  84.   
  85.         public long getChildId(int groupPosition, int childPosition) {   
  86.             return childPosition;   
  87.         }   
  88.   
  89.         public int getChildrenCount(int groupPosition) {   
  90.             return children[groupPosition].length;   
  91.         }   
  92.   
  93.         public TextView getGenericView() {   
  94.             // Layout parameters for the ExpandableListView   
  95.             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(   
  96.                     ViewGroup.LayoutParams.MATCH_PARENT, 64);   
  97.   
  98.             TextView textView = new TextView(ExpandableList1.this);   
  99.             textView.setLayoutParams(lp);   
  100.             // Center the text vertically   
  101.             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);   
  102.             // Set the text starting position   
  103.             textView.setPadding(36000);   
  104.             return textView;   
  105.         }   
  106.            
  107.         public View getChildView(int groupPosition, int childPosition, boolean isLastChild,   
  108.                 View convertView, ViewGroup parent) {   
  109.             TextView textView = getGenericView();   
  110.             textView.setText(getChild(groupPosition, childPosition).toString());   
  111.             return textView;   
  112.         }   
  113.   
  114.         public Object getGroup(int groupPosition) {   
  115.             return groups[groupPosition];   
  116.         }   
  117.   
  118.         public int getGroupCount() {   
  119.             return groups.length;   
  120.         }   
  121.   
  122.         public long getGroupId(int groupPosition) {   
  123.             return groupPosition;   
  124.         }   
  125.   
  126.         public View getGroupView(int groupPosition, boolean isExpanded, View convertView,   
  127.                 ViewGroup parent) {   
  128.             TextView textView = getGenericView();   
  129.             textView.setText(getGroup(groupPosition).toString());   
  130.             return textView;   
  131.         }   
  132.   
  133.         public boolean isChildSelectable(int groupPosition, int childPosition) {   
  134.             return true;   
  135.         }   
  136.   
  137.         public boolean hasStableIds() {   
  138.             return true;   
  139.         }   
  140.   
  141.     }   
  142. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值