안드로이드 앱 개발

Expandable Cardview (카드뷰 접었다가 펴는 기능) - Kotlin 본문

카테고리 없음

Expandable Cardview (카드뷰 접었다가 펴는 기능) - Kotlin

스텝바이스텝안드로이드 2021. 5. 11. 01:38

먼저, xml에서

default로 보여지는 애들과 숨겨져 있는 애들을 구분한다.

<com.google.android.material.card.MaterialCardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        app:layout_constraintTop_toBottomOf="@+id/customer_number_tv"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        app:elevation="0dp">

        <!--This is a ConstraintLayout for the entire CardView
                   including the expandable portion-->

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">



            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/fixed_layout"
                android:layout_width="match_parent"
                android:layout_height="30dp"

                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:text="방문이력"
                android:textColor="@color/mainGrey"
                android:fontFamily="@font/notosanscjkkrmedium"
                android:includeFontPadding="false"

                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginLeft="10dp" />

            <ImageView
                android:id="@+id/history_expand_iv"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginEnd="10dp"
                android:adjustViewBounds="true"
                android:scaleType="center"
                app:srcCompat="@drawable/outline_expand_more_black_36"
                app:tint="@color/mainGrey"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <LinearLayout
            android:id="@+id/hiden_view"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            tools:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fixed_layout">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:text="Database Management"
                android:textColor="#000000"
                android:textSize="20dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:text="Database Management"
                android:textColor="#000000"
                android:textSize="20dp" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:text="Database Management"
                android:textColor="#000000"
                android:textSize="20dp" />

        </LinearLayout>



        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.card.MaterialCardView>

 

2. 다음 코드에서 

클릭 이벤트 처리

binding.fixedLayout.setOnClickListener {
            if (binding.hidenView.visibility == View.VISIBLE){
                TransitionManager.beginDelayedTransition(binding.cardview,
                    AutoTransition())
                binding.hidenView.visibility = View.GONE

                binding.historyExpandIv.setImageResource(R.drawable.arrow_down_float)
            } else{
                TransitionManager.beginDelayedTransition(binding.cardview,
                    AutoTransition())
                binding.hidenView.visibility = View.VISIBLE
                binding.historyExpandIv.setImageResource(R.drawable.arrow_up_float)
            }
        }