多级ExpandleListView? Android的

时间:2016-06-05 23:03:31

标签: java android listview expandablelistview

嗨我需要一个像这样的列表我现在使用expandlelist但我不知道如何获得主要像我需要的子列表。 V只是显示下拉箭头

标题五 问题五 回答 回答 回答 问题五 回答 回答 答案

标题五 问题五 回答 回答 答案

1 个答案:

答案 0 :(得分:0)

正在实现这样的事情,使用AnimatedExpandableListView使用一个以递归方式重新创建自身的适配器。

您需要数据:

private HashMap<CategoryModel, List<CategoryModel>> mMainMenuExpandableList; // this is hashmap for categories;

这是适配器的构造函数:

public CategoriesExpandableListAdapter(Context context, HashMap<CategoryModel, List<CategoryModel>> hashMap, int parentCategoryId, int paddingLevel){ //... parent category is category from which start adapter (you search for it and init adapter according it, padding level i used to make padding from left for sub categories)

现在好了:

对于子类别,您需要2个视图,其中一个是最终的,没有子类别,另一个是包含AnimatedExpandableListView的视图,您在创建视图持有者中为它创建了另一个适配器。我将此列表可见性设置为已消失,并仅在用户点击它时显示它,并应用自定义下拉动画。

这是带有可扩展列表的项目示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_container"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:focusable="false"
    android:clickable="true"
    android:background="@drawable/custom_background"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/item_layout"
        android:layout_width="match_parent"
        android:focusable="false"

        android:layout_height="wrap_content">
        <com..........customModels.TextViewMedium
            android:id="@+id/categoryTitle"
            android:focusable="false"
            android:layout_gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center_vertical"
            android:layout_marginRight="48dp"
            android:textSize="18dp"
            android:text="test text"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/item_folder_icon"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginLeft="4dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_folder"/>
    <ImageView
        android:id="@+id/item_folder_right_arrow"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_rightarow"/>
    <View
        android:id="@+id/item_divider"
        android:layout_below="@id/item_layout"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divider"/>
    <LinearLayout
        android:layout_below="@+id/item_divider"
        android:id="@+id/main_menu_expandable_sublist_container"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:visibility="gone"
        android:layout_height="wrap_content">
        <com..........customModels.AnimatedExpandableListView
            android:id="@+id/main_menu_expandable_sublist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@color/divider"
            android:childDivider="@color/divider"
            android:transcriptMode="normal"
            android:visibility="gone"
            android:groupIndicator="@null"
            android:background="@color/layouts_background"/>
    </LinearLayout>

</RelativeLayout>

这里是最终项目的样本(没有子类):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_container"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:focusable="false"
    android:clickable="true"
    android:background="@drawable/custom_background"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/item_folder_right_arrow"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_rightarow"/>
    <LinearLayout
        android:id="@+id/item_layout"
        android:layout_width="match_parent"
        android:focusable="false"

        android:layout_height="match_parent">
        <com...........customModels.TextViewMedium
            android:id="@+id/categoryTitle"
            android:focusable="false"
            android:layout_gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center_vertical"
            android:layout_marginRight="48dp"
            android:textSize="18dp"
            android:text="test text"/>
    </LinearLayout>
    <View
        android:layout_below="@id/item_layout"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divider"/>
</RelativeLayout>

实施起来并不容易,但最终结果就像魅力一样))

相关问题