【问题标题】:LinearLayout with RecyclerView hide ImageView on scroll outside CardView带有 RecyclerView 的 LinearLayout 在 CardView 外滚动时隐藏 ImageView
【发布时间】:2018-11-04 16:00:48
【问题描述】:

我正在尝试在 cardview 中使用 recyclerview 进行活动。在cardview上方有一个图像,当我滚动recyclerview时我希望它消失但不让浅蓝色框消失。我怎样才能做到这一点? 我尝试使用 NestedScrollView 但没有结果

Actual layout

这是我的布局代码

<?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"
          android:transitionName="itemMainTransition"
          tools:context=".activity.WeatherActivity">
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

              <ImageView
                  android:id="@+id/meteoTrentinoBanner"
                  android:background="#42a4ce"
                  android:src="@drawable/logo_meteotn"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />

              <android.support.v7.widget.CardView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginEnd="5dp"
                  android:layout_marginStart="5dp"
                  app:cardCornerRadius="5dp"
                  app:cardElevation="5dp"
                  app:cardUseCompatPadding="true">

             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:orientation="vertical">

                 <LinearLayout
                     android:layout_width="match_parent"
                     android:layout_height="55dp"
                     android:orientation="horizontal"
                     android:background="@drawable/weather_border">

                     <ImageView
                         android:id="@+id/backDay"
                         android:layout_width="70dp"
                         android:layout_height="match_parent"
                         android:layout_gravity="center_vertical"
                         android:src="@drawable/ic_back"/>
                     <LinearLayout
                         android:layout_width="0dp"
                         android:layout_height="match_parent"
                         android:layout_weight="1"
                         android:orientation="horizontal">
                         <TextView
                             android:id="@+id/textViewMinTemp"
                             android:layout_width="40dp"
                             android:layout_height="match_parent"
                             android:textAlignment="center"
                             android:gravity="center_vertical|center_horizontal"
                             android:textSize="19sp"
                             android:textStyle="bold"
                             android:textColor="#0000FF"/>
                         <TextView
                             android:id="@+id/textViewDay"
                             android:text="@string/oggi"
                             android:layout_width="0dp"
                             android:layout_height="match_parent"
                             android:layout_weight="1"
                             android:gravity="center_vertical"
                             android:textAlignment="center"
                             android:textSize="25sp"
                             android:textStyle="bold"
                             tools:ignore="NestedWeights" />
                         <TextView
                             android:id="@+id/textViewMaxTemp"
                             android:layout_width="40dp"
                             android:layout_height="match_parent"
                             android:textAlignment="center"
                             android:gravity="center_vertical|center_horizontal"
                             android:textSize="19sp"
                             android:textStyle="bold"
                             android:textColor="#FF0000"/>
                     </LinearLayout>

                     <ImageView
                         android:id="@+id/forwardDay"
                         android:layout_width="70dp"
                         android:layout_height="match_parent"
                         android:layout_gravity="center_vertical"
                         android:src="@drawable/ic_forward"/>
                 </LinearLayout>

                 <LinearLayout
                     android:layout_width="match_parent"
                     android:layout_height="55dp"
                     android:orientation="horizontal"
                     android:background="@drawable/weather_border">
                     <ImageView
                         android:id="@+id/meteoInfo"
                         android:layout_width="40dp"
                         android:layout_height="match_parent"
                         android:src="@drawable/ic_info"/>

                     <TextView
                         android:id="@+id/textViewWeather"
                         android:textSize="23sp"
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         android:gravity="center_vertical"
                         android:singleLine="true"
                         android:ellipsize="marquee"
                         android:marqueeRepeatLimit="marquee_forever"
                         android:focusable="true"
                         android:focusableInTouchMode="true"
                         android:scrollHorizontally="true"/>
                 </LinearLayout>
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerViewMeteo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                </android.support.v7.widget.RecyclerView>
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

  • 您的回收站视图是否应该滚动到蓝色框上?
  • 不,蓝色框应该始终可见,图像应该在滚动回收视图时消失。卡片视图应转换为顶部,隐藏图像
  • 现在的行为是什么?
  • 目前图像和盒子都固定了,只有recyclerview滚动

标签: android xml layout android-recyclerview imageview


【解决方案1】:
<?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=".LeaveActivity">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/colorPrimary" />

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:contentDescription="@string/app_name"
                android:src="@mipmap/ic_launcher"
                app:layout_collapseMode="parallax" />
        </android.support.design.widget.CollapsingToolbarLayout>

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


    <LinearLayout
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:orientation="vertical">


        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="8dp"
            app:cardElevation="5dp"
            app:cardPreventCornerOverlap="true"
            app:cardUseCompatPadding="true">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:fillViewport="true"
                android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/resView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </android.support.v4.widget.NestedScrollView>


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

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

【讨论】:

  • 这样更好,但带有箭头的浅蓝色框仍然消失。如何在滚动时将其固定到顶部并仅滚动回收器视图?这是当前行为的屏幕imgur.com/a/dSW30NJimgur.com/a/OMgGsDKimgur.com/a/kvYYzGT
  • 好的,我已经编辑了答案,检查一下,让我知道
  • 现在运行良好,非常感谢,这是我接受的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 2011-07-26
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
相关资源
最近更新 更多