Android中ExpandableListView特殊属性的玩法

来源:互联网 发布:php简单计算器 编辑:程序博客网 时间:2024/04/28 23:06

1. 设置ExpandableListView 默认是展开的:

先实例化 exListView

然后

   exListView.setAdapter(exlvAdapter);

   //遍历所有group,将所有项设置成默认展开

   int groupCount = exListView.getCount();

   for (int i=0; i<groupCount; i++) {

       exListView.expandGroup(i);

       };


 

2. 去掉ExpandableListView 默认的箭头

  用到ExpandableListView时有个箭头图标系统自带的在你自定义布局也不能去掉只要设置一个属性即可,如下:

  settingLists.setGroupIndicator(null);  ~~~~~~~~~~~~~~~~~此处就是设置自定义的箭头图标的。置空则没有了。


也可以自定义(但是位置还是在那个地方不推荐)如下:

首先,自定义一个expandablelistviewselector.xml文件,具体内容如下: 
Java代码

<?xml version="1.0" encoding="utf-8"?>   

<selector xmlns:android="http://schemas.android.com/apk/res/android">   

    <item android:state_expanded="true" android:drawable="@drawable/expandablelistviewindicatordown" />   

    <item android:drawable="@drawable/expandablelistviewindicator" />   

</selector>  

加一句代码如下:

settingLists.setGroupIndicator(this.getResources().getDrawable(R.drawable.expandablelistviewselector)); 

大功告成

3. 将默认的箭头修改到右边显示:


1首先 ExpandableListView elistview;

         elistview.setGroupIndicator(null);//将控件默认的左边箭头去掉,

2在 自定义的继承自BaseExpandableListAdapter的adapter中有一个方法

/**
 * 父类view
 */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        Log.i("zhaoxiong","parent view");
            LinearLayout parentLayout=(LinearLayout) View.inflate(context, R.layout.wowocoupons_parent_item, null);
            TextView parentTextView=(TextView) parentLayout.findViewById(R.id.parentitem);
            parentTextView.setText(parentlist.get(groupPosition));
            ImageView parentImageViw=(ImageView) parentLayout.findViewById(R.id.arrow);

//判断isExpanded就可以控制是按下还是关闭,同时更换图片
            if(isExpanded){
                parentImageViw.setBackgroundResource(R.drawable.arrow_down);
            }else{
                parentImageViw.setBackgroundResource(R.drawable.arrow_up);
            }

        
        return parentLayout;
    }

0 0
原创粉丝点击