如何在Android中创建静态(仅使用xml文件)ExpandableListView

时间:2021-10-13 14:31:03

I need to insert GroupData and ChildData directly into ExpandableListView in xml file itself without using java. Java file should only for showing xml file not for inserting data.

我需要在不使用java的情况下将GroupData和ChildData直接插入到xml文件本身的ExpandableListView中。 Java文件应仅用于显示xml文件而不是用于插入数据。

Our output will be :

我们的输出将是:

Group 1 -> Child 11, Child 12, Child 13

第1组 - >儿童11,儿童12,儿童13

Group 2 -> Child 21, Child 22, Child 23

第2组 - > 21岁儿童,22岁儿童,23岁儿童

Group 3 -> Child 31, Child 32, Child 33

第3组 - > 31岁儿童,32岁儿童,33岁儿童

Thanks in Advance

提前致谢

2 个解决方案

#1


6  

I don't think so it is possible . You must have to write code for generating listview using java file.

我认为不可能。您必须使用java文件编写用于生成listview的代码。

Look Here for shortest way to generate expandable listview.

在这里查看生成可扩展列表视图的最短路径。

#2


0  

If you don't want to use ExpandableListview because of java code to set adapter and all then You can use a cardview which is expand and collapse. This is the simplest way for static(XML) collapse and expand feature.

如果您不想使用ExpandableListview,因为java代码设置适配器,那么您可以使用扩展和折叠的cardview。这是静态(XML)折叠和扩展功能的最简单方法。

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:elevation="3dp"
        app:cardCornerRadius="0dp">

        <LinearLayout
                android:id="@+id/group_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/group_one"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:lines="1"
                    android:text="Group 1"
                    android:textStyle="bold" />

                <LinearLayout
                    android:id="@+id/child_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/child_one"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:lines="1"
                        android:text="Child 11" />

                    <TextView
                        android:id="@+id/child_two"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:lines="1"
                        android:text="Child 12" />

                    <TextView
                        android:id="@+id/child_three"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:lines="1"
                        android:text="Child 13" />

                </LinearLayout>
            </LinearLayout>
</android.support.v7.widget.CardView>

Write the following code in onClickListener

在onClickListener中编写以下代码

 cardView = (CardView) findViewById(R.id.card_view);
 childContainer = (LinearLayout) findViewById(R.id.child_container);
 boolean flag = false;

    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (flag == true) {
                TransitionManager.beginDelayedTransition(cardView);
                childContainer.setVisibility(View.GONE);
                flag = false;
            } else if (flag == false) {
                TransitionManager.beginDelayedTransition(cardView);
                childContainer.setVisibility(View.VISIBLE);
                flag = true;
            }
        }
    });

This is for one group and you can duplicate this code for more groups.

这适用于一个组,您可以为更多组复制此代码。

#1


6  

I don't think so it is possible . You must have to write code for generating listview using java file.

我认为不可能。您必须使用java文件编写用于生成listview的代码。

Look Here for shortest way to generate expandable listview.

在这里查看生成可扩展列表视图的最短路径。

#2


0  

If you don't want to use ExpandableListview because of java code to set adapter and all then You can use a cardview which is expand and collapse. This is the simplest way for static(XML) collapse and expand feature.

如果您不想使用ExpandableListview,因为java代码设置适配器,那么您可以使用扩展和折叠的cardview。这是静态(XML)折叠和扩展功能的最简单方法。

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:elevation="3dp"
        app:cardCornerRadius="0dp">

        <LinearLayout
                android:id="@+id/group_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/group_one"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:lines="1"
                    android:text="Group 1"
                    android:textStyle="bold" />

                <LinearLayout
                    android:id="@+id/child_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/child_one"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:lines="1"
                        android:text="Child 11" />

                    <TextView
                        android:id="@+id/child_two"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:lines="1"
                        android:text="Child 12" />

                    <TextView
                        android:id="@+id/child_three"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:lines="1"
                        android:text="Child 13" />

                </LinearLayout>
            </LinearLayout>
</android.support.v7.widget.CardView>

Write the following code in onClickListener

在onClickListener中编写以下代码

 cardView = (CardView) findViewById(R.id.card_view);
 childContainer = (LinearLayout) findViewById(R.id.child_container);
 boolean flag = false;

    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (flag == true) {
                TransitionManager.beginDelayedTransition(cardView);
                childContainer.setVisibility(View.GONE);
                flag = false;
            } else if (flag == false) {
                TransitionManager.beginDelayedTransition(cardView);
                childContainer.setVisibility(View.VISIBLE);
                flag = true;
            }
        }
    });

This is for one group and you can duplicate this code for more groups.

这适用于一个组,您可以为更多组复制此代码。