首页 > 解决方案 > "Shrink" CardView while scrolling in another view

问题描述

Screenshot

enter image description here

As you can see, the layout is a bit too big for the older device. My idea is to shrink the CardView while the user is scrolling in the white area (ViewPager) to make it bigger. I have seen it working well in another app (can't remember which). I have no idea how I can do it, do I need write it programmatically?

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WordActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="20dp"
        android:visibility="gone" />

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="0dp"
        app:cardBackgroundColor="@color/primary_dark"
        app:cardCornerRadius="0dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp">

            <TextView
                android:id="@+id/wordTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/poweredBy"
                android:layout_centerInParent="true"
                android:layout_marginTop="-12dp"
                android:fontFamily="@font/poppins"
                android:text="@string/word_title"
                android:textColor="@android:color/white"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/wordTranslated"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/wordTitle"
                android:layout_alignStart="@id/wordTitle"
                android:layout_marginTop="-10dp"
                android:fontFamily="@font/poppins"
                android:textColor="@android:color/darker_gray"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/poweredBy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:fontFamily="@font/bungee_hairline"
                android:text="@string/powered"
                android:textColor="@android:color/darker_gray"
                android:textSize="16sp" />

        </RelativeLayout>

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

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/cardView">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@color/primary"
            android:elevation="5dp"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="2dp"
            app:tabPaddingEnd="-1dp"
            app:tabPaddingStart="-1dp"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@color/white_transparent" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="45dp" />

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

标签: androidxmlandroid-studio

解决方案


它可能不适用于您当前的布局,但如果您可以集成 Android 的折叠工具栏,您可能会节省一些时间。

否则,您可以使用类似的方法来检测用户何时位于列表顶部并显示视图,否则将其隐藏


推荐阅读