0

I have a RecyclerView that inflates CardView with a custom LinearLayoutManager in which scrolling is disabled. I'll always only display 2 CardView on the screen. Each CardView has a collapsed part that appears when the little arrow is clicked.

Problem: the next card is pushed down

What I need: on the top CardView, the collpased part should overlap onto the bottom Cardview. I already have an animation to expand it. On the bottom one, I want to overlap the picture onto the top CardView. Also, the other none visible CardViews should never appear in the process. How do I achieve that?

Here is my CardView:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        card_view:cardElevation="8dp"
        card_view:cardCornerRadius="4dp">


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

        <!--*************visible part****************************-->

        <ImageView
                android:id="@+id/clash_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                card_view:layout_constraintTop_toTopOf="parent"
                card_view:layout_constraintBottom_toTopOf="@id/tv1"
                android:src="@drawable/image_placeholder" card_view:layout_constraintEnd_toEndOf="parent"
                card_view:layout_constraintStart_toStartOf="parent"
                />

        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:paddingLeft="5dp"
                  android:paddingRight="5dp"
                  android:text="#Hashtag1"
                    android:id="@+id/tv1"
                card_view:layout_constraintTop_toBottomOf="@+id/clash_image"
                card_view:layout_constraintStart_toStartOf="parent"/>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:paddingLeft="5dp"
                  android:paddingRight="5dp"
                  android:text="#Hashtag1"
                  android:id="@+id/tv2"
                card_view:layout_constraintStart_toEndOf="@+id/tv1"
                card_view:layout_constraintTop_toBottomOf="@+id/clash_image"/>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:paddingLeft="5dp"
                  android:paddingRight="5dp"
                  android:text="#Hashtag1"
                  android:id="@+id/tv3"
                  card_view:layout_constraintStart_toEndOf="@+id/tv2"
                  card_view:layout_constraintTop_toBottomOf="@+id/clash_image"/>

        <ImageView
                android:id="@+id/expand"
                android:src="@drawable/ic_expand"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                card_view:layout_constraintEnd_toEndOf="parent"
                card_view:layout_constraintTop_toBottomOf="@+id/clash_image"/>


        <!--*****************invisible part to expand on click****************************-->


        <RelativeLayout
                android:id="@+id/rlCollapsingPart"
                android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        card_view:layout_constraintTop_toBottomOf="@+id/expand"
                        card_view:layout_constraintStart_toStartOf="parent"
        android:visibility="gone">

            <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/avatarThumbnail"
                    style="@style/icon"
                    android:src="@drawable/defaultavatar"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"/>

            <TextView
                    android:id="@+id/username_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="willsmith"
                    android:textColor="@color/black"
                    android:textStyle="bold"
                    android:layout_toEndOf="@+id/avatarThumbnail"
            />



            <TextView
                    android:id="@+id/caption_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingStart="10dp"
                    android:textColor="@color/black"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/avatarThumbnail"
                    tools:text="willsmith This is a caption for the post. It's actually a very long caption."/>


            <RelativeLayout android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/caption_text"
            android:background="@color/grey">

                <ImageView
                        android:id="@+id/like_image"
                        android:layout_height="45dp"
                        android:layout_width="45dp"
                        android:padding="10dp"
                        android:src="@drawable/ic_home"
                        android:layout_alignParentStart="true"/>

                <ImageView
                        android:id="@+id/comment_image"
                        card_view:layout_constraintStart_toEndOf="@id/like_image"
                        card_view:layout_constraintTop_toTopOf="@id/like_image"
                        card_view:layout_constraintBottom_toBottomOf="@id/like_image"
                        android:layout_height="45dp"
                        android:layout_width="45dp"
                        android:padding="10dp"
                        android:src="@drawable/ic_home"
                        android:layout_toEndOf="@+id/like_image"/>

                <ImageView
                        android:id="@+id/share_image"
                        android:layout_toEndOf="@+id/comment_image"
                        android:layout_height="45dp"
                        android:layout_width="45dp"
                        android:padding="10dp"
                        android:src="@drawable/ic_home"/>

                <ImageView
                        android:id="@+id/more_image"
                        style="@style/icon"
                        android:src="@drawable/ic_home"
                        android:layout_alignParentEnd="true"/>

            </RelativeLayout>

        </RelativeLayout>





    </androidx.constraintlayout.widget.ConstraintLayout>

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

EDIT

I expand the hidden part using a basic on click listener in my adapter and an anim. Adapter.kt

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

...         

holder.view.expand.setOnClickListener(View.OnClickListener {
        val slideDown = AnimationUtils.loadAnimation(context, R.anim.card_expand_animator)
        //toggling visibility
        if(holder.view.rlCollapsingPart.visibility == View.VISIBLE) {
            holder.view.rlCollapsingPart.visibility = View.GONE
        }else{
            holder.view.rlCollapsingPart.startAnimation(slideDown)
            holder.view.rlCollapsingPart.visibility = View.VISIBLE    
        }
    })  

}

MyAnim.xml (in anim folder)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
            android:duration="500"
            android:fromXScale="1.0"
            android:fromYScale="0.0"
            android:toXScale="1.0"
            android:toYScale="1.0"/>
</set>

normal state

expanded state

Community
  • 1
  • 1
mrj
  • 589
  • 1
  • 7
  • 17
  • Please provide how you are expanding the `CardView` – HB. Jun 17 '19 at 04:04
  • I just updated it – mrj Jun 17 '19 at 10:54
  • The problem is the view that you are animation (`rlCollapsingPart`) is part of your `MaterialCardView`. Currently, the `MaterialCardView` is basically just expanding in height, pushing the one below it down. You will have to create a new layout containing the view you want to expand. I would recommend you implement a `PopupMenu` that inflates a custom layout. I could not find a kotlin example, but you would be able to understand - https://stackoverflow.com/questions/49706495/how-to-pass-a-custom-layout-to-a-popupmenu – HB. Jun 17 '19 at 11:12
  • That may work to expand the collapsed part of my first Card over the second Card, but not for my second need: upon clicking the bottom expand button, I want to push the image of the second Card over the first one to reveal the collapsed part of the second Card :( – mrj Jun 17 '19 at 11:20
  • `I want to push the image of the second Card over the first one ` what? why would you want to do that? Then you will not be able to see the first one if you "push it over the first one". – HB. Jun 17 '19 at 11:52
  • Yes that's exactly right :). The second card will cover half of the first one. If the part is collapsed again (another click), then the first card will be visible again. – mrj Jun 17 '19 at 12:08
  • have you figured this out ? i want to do something similar – Sandeep Oct 21 '19 at 19:00
  • @Sandeep see my answer below. As is, it's not possible. I opted for a simpler solution that works well enough: on click, the hidden part expands upwards, that is on top of the content. The only other solution would have been using a `PopUpWindow`. – mrj Oct 22 '19 at 14:49

1 Answers1

0

After further research and understanding, this is not possible as is. To "overlap" another card, I have to inflate a new layout using PupUpWindow, which can look smooth using the Shared Element principle. Otherwise the LayoutManager will always push the other Card down. For now I chose to go for an easier solution, which is to expand the hidden part towards to top, to cover the Card's Image.

mrj
  • 589
  • 1
  • 7
  • 17