ExpandableListView设置选中child的背景

转自:http://blog.csdn.net/hknock/article/details/12914915


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@drawable/ract_bg"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ExpandableListView  
  9.         android:id="@+id/listview"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:childDivider="#885D7081"  
  13.         android:choiceMode="singleChoice"  
  14.         android:divider="#88455869"  
  15.         android:dividerHeight="1px"  
  16.         android:groupIndicator="@null"  
  17.         android:listSelector="#00000000"  
  18.         android:scrollingCache="false" />  
  19.   
  20. </LinearLayout>  


说明:需要定义android:listSelector="#00000000"关闭点击的效果,android:choiceMode="singleChoice"设为单选模式


charge_expandable_fragment_group_item.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="60dp"  
  5.     android:background="#88506475"  
  6.     android:gravity="center_vertical"  
  7.     android:minHeight="60dp"  
  8.     android:orientation="horizontal" >  
  9.   
  10.     <ImageView  
  11.         android:id="@+id/group_indicator"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginLeft="10dp"  
  15.         android:layout_marginRight="10dp"  
  16.         android:src="@drawable/expander_arrow_collapse" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/group_textview"  
  20.         style="@style/WhiteLargeNormalStyle"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:gravity="left|center_vertical"  
  24.         android:text="父元素" />  
  25.   
  26. </LinearLayout>  

charge_expandable_fragment_child_item.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="80dp"  
  5.     android:background="@drawable/selector_expandable_child_item"  
  6.     android:gravity="center_vertical"  
  7.     android:minHeight="80dp"  
  8.     android:orientation="horizontal" >  
  9.   
  10.     <TextView  
  11.         android:id="@+id/child_textview"  
  12.         style="@style/WhiteLargeNormalStyle"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_marginLeft="20dp"  
  16.         android:layout_weight="1"  
  17.         android:gravity="left|center_vertical"  
  18.         android:text="子元素" />  
  19.   
  20.     <ImageView  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_marginRight="5dp"  
  24.         android:src="@drawable/arrow_right" />  
  25.   
  26. </LinearLayout>  


selector_expandable_child_item.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/child_item2" android:state_activated="true"/>  
  5.     <item android:drawable="@drawable/child_item1"/>  
  6.   
  7. </selector>  

代码部分:

[java]  view plain copy
  1. final private ExpandableListView.OnChildClickListener mOnClickListener = new ExpandableListView.OnChildClickListener() {  
  2.   
  3.     @Override  
  4.     public boolean onChildClick(ExpandableListView parent, View v,  
  5.             int groupPosition, int childPosition, long id) {  
  6.         // 设置选中背景  
  7.         setItemChecked(groupPosition, childPosition);  
  8.         // TODO 执行选中后的操作  
  9.         return true;  
  10.     }  
  11. };  
  12.   
  13. private void setItemChecked(int groupPosition, int childPosition) {  
  14.     if (mListView == null) {  
  15.         return;  
  16.     }  
  17.   
  18.     this.mGroupPosition = groupPosition;  
  19.     this.mChildPosition = childPosition;  
  20.   
  21.     int numberOfGroupThatIsOpened = 0;  
  22.     for (int i = 0; i < groupPosition; i++) {  
  23.         if (mListView.isGroupExpanded(i)) {  
  24.             numberOfGroupThatIsOpened += mAdapter.getChildrenCount(i);  
  25.         }  
  26.     }  
  27.     int position = numberOfGroupThatIsOpened + groupPosition  
  28.             + childPosition + 1;  
  29.     System.out.println("groupPosition=" + groupPosition  
  30.             + ", childPosition=" + childPosition + ", position="  
  31.             + position + ", isItemChecked="  
  32.             + mListView.isItemChecked(position));  
  33.     if (!mListView.isItemChecked(position)) {  
  34.         mListView.setItemChecked(position, true);  
  35.     }  
  36. }  

[java]  view plain copy
  1. @Override  
  2. public View getGroupView(int groupPosition, boolean isExpanded,  
  3.         View convertView, ViewGroup parent) {  
  4.     if (convertView == null) {  
  5.         convertView = mInflater.inflate(  
  6.                 R.layout.charge_expandable_fragment_group_item,  
  7.                 null);  
  8.     }  
  9.     ImageView indicator = (ImageView) convertView  
  10.             .findViewById(R.id.group_indicator);  
  11.     if (isExpanded) {  
  12.         // 设置展开后的group标头图片  
  13.         indicator  
  14.                 .setImageResource(R.drawable.expander_arrow_expand);  
  15.         // 在折叠后再次展开,之前的选中会消失,此处需要设置一次  
  16.         if (groupPosition == mGroupPosition) {  
  17.             setItemChecked(mGroupPosition, mChildPosition);  
  18.         }  
  19.     } else {  
  20.         // 折叠后的group标头图片  
  21.         indicator  
  22.                 .setImageResource(R.drawable.expander_arrow_collapse);  
  23.     }  
  24.     TextView text = (TextView) convertView  
  25.             .findViewById(R.id.group_textview);  
  26.     text.setText(groups[groupPosition]);  
  27.     return convertView;  

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值