expandablelistview用于展开/折叠的不同样式

时间:2015-03-02 14:02:45

标签: android expandablelistview

我正在尝试获取自定义的Expandablelistview,它具有不同的布局,可以展开/折叠状态...

适配器

@Override

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

String headerTitle = ((User) getGroup(groupPosition)).getName();
if (convertView == null) {
    LayoutInflater infalInflater = (LayoutInflater) this.con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(isExpanded) {
        Log.e("DBG", "isExpanded");
        convertView = infalInflater.inflate(R.layout.expanablelistview_header_expanded, parent, false);
    } else {
        Log.e("DBG", "isNotExpanded");
        convertView = infalInflater.inflate(R.layout.expanablelistview_header_collapse, parent, false);
    }
}

TextView lblListHeader = (TextView) convertView.findViewById(R.id.tv_user);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);

return convertView;
}

expanablelistview_header_collapse.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/xml_border"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_user"
            android:textSize="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <View 
        android:background="@color/transparent"
        android:layout_width="match_parent" 
        android:layout_height="10dp" />

</LinearLayout>

expanablelistview_header_expanded.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/xml_border_top"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_user"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

这是我的尝试,但这个方法仅在启动时调用,而不是在扩展状态更改...

谢谢你的想法!

2 个答案:

答案 0 :(得分:1)

问题是

if (convertView == null)

谢谢!

答案 1 :(得分:-1)

Try loading the layouts in MainActivity in

<ExpandableListView Obj>.setOnGroupExpandListener();

and

<ExpandableListView Obj>.setOnGroupCollapseListener();