Balaji
Balaji

Reputation: 2026

How to Create Static (Using xml file only) ExpandableListView in Android

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.

Our output will be :

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

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

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

Thanks in Advance

Upvotes: 4

Views: 1757

Answers (2)

Ekta Bhawsar
Ekta Bhawsar

Reputation: 746

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.

<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

 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.

Upvotes: 0

Chirag
Chirag

Reputation: 56935

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

Look Here for shortest way to generate expandable listview.

Upvotes: 6

Related Questions