【问题标题】:Multi Level ExpandleListView ? Android多级扩展列表视图?安卓
【发布时间】:2016-06-05 23:03:31
【问题描述】:

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

第五篇 问题五 回答 回答 回答 问题五 回答 回答 回答

第五篇 问题五 回答 回答 回答

【问题讨论】:

标签: java android listview expandablelistview


【解决方案1】:

正在实现这样的事情,正在使用 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>

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

【讨论】:

  • 所以除了 xml 这将是适配器的代码吗?
  • 这是可展开的recyclerview的项目
猜你喜欢
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 2023-04-07
  • 2019-02-17
相关资源
最近更新 更多