How to close nested layout when open new layout in

2019-08-31 16:48发布

问题:

I am developing an android application,In that I am using a Layout that contains a root layout(invitation_single) and nested layout(hidden). When I am on click root layout the nested layout display along with root layout that works fine. now my output is display like below image

but i am expecting my output as,

that means if am click "event2" then simultaneously nested layout(yes,no,maybe buttons) of first "event" needs to hide,but if i have a single event then the nested layout of that event needs to be visible,else if i have more than one event means i want to display my output as second image(nested layout of first"event" hide when click second event). How can i achieve that. my layout code is looking below,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/invitation_single"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="?android:dividerVertical"
    android:dividerPadding="5dp"
    android:showDividers="middle"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_action_event" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:clickable="false"
        android:focusable="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/invitation_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="0dp"
            android:paddingTop="3dp"
            android:textColor="@color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/invitation_place"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="0dp"
            android:textColor="@color/black"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/hidden"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginLeft="-270dp"
        android:layout_marginTop="60dp"
        android:layout_weight="1"
        android:clickable="true"
        android:focusable="true"
        android:orientation="horizontal"
        android:paddingTop="1dp"
        android:visibility="gone"
        android:weightSum="3">


        <Button
            android:id="@+id/yesbutton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="7dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:text="Yes"
            android:textColor="@color/black"></Button>

        <Button
            android:id="@+id/nobutton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="25dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:text="No"
            android:textColor="@color/black"></Button>

        <Button
            android:id="@+id/buttonmaybe"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:text="Maybe"
            android:textColor="@color/black"></Button>

    </LinearLayout>


</LinearLayout>

my programming code is looking below,

final LinearLayout first = (LinearLayout) convertView.findViewById(R.id.invitation_single);
            final LinearLayout second = (LinearLayout) convertView.findViewById(R.id.hidden);
first.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.invitation_single:
                            second.setVisibility(View.VISIBLE);
                            break;

                    }
                }


            });

In above code I am used to display nested layout(hidden) immediately below root layout(invitation single).

回答1:

Do you have a small set of events or the number of events could get pretty long? If the second choice is true then I would suggest looking at ExpandableListView. Check out this link particularly on how to collapse the previously open items upon expanding a new one: ExpandabeListView auto collapse