繁体   English   中英

如何滚动 Cardview 并在 Android 的 ToolBar 下方附加 TabLayout?

[英]How do i Scroll Cardview and attach TabLayout below ToolBar in Android?

我正在尝试滚动卡片视图的内容,当滚动完成后,我在卡片视图下方有包含选项卡布局的视图寻呼机,我想在工具栏下方附加此视图寻呼机。

例如:我在滚动前的布局结构

----------------
    ToolBar
----------------
   Cardview

   contents
      . 
      .
      .
      .
----------------
   ViewPager

   TabLayout
-----------------

当这个卡片视图内容滚动时,我想要如下结构。

----------------
    ToolBar
----------------
   ViewPager

   TabLayout
-----------------

当卡片视图完全滚动时,我试图将我的 ViewPager 附加到 ToolBar 下方。 我设计了.xml 布局如下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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:background="#e6e6e6"
    tools:context=".ViewerActivity">

    <androidx.appcompat.widget.Toolbar
        app:title="Post View"
        android:id="@+id/PostViewerToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="ScrollViewCount">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                    <ImageView
                        android:layout_marginTop="10dp"
                        android:id="@+id/SharedImg"
                        android:layout_gravity="center_horizontal"
                        android:adjustViewBounds="true"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

                    <View
                        android:layout_marginTop="10dp"
                        android:background="#f1f1f1"
                        android:layout_width="match_parent"
                        android:layout_height="2dp"/>
                    <TextView
                        android:layout_marginTop="10dp"
                        android:id="@+id/DescriptionTv"
                        android:text="Post description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
                </LinearLayout>
            </androidx.cardview.widget.CardView>
        </LinearLayout>

    </ScrollView>


    <androidx.viewpager.widget.ViewPager
        android:id="@+id/Viewpager"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/InvolveTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </androidx.viewpager.widget.ViewPager>
</LinearLayout>

这个怎么做。 请帮忙。

您可以尝试将CardView内容放在CollapsingToolbarLayout中。 因此,当您滚动您的 CardView 并且内容崩溃时。

[更新] 我做了一些改变。 只需根据您的要求更改视图的来源、值和设计。 看起来像这样 您可以根据自己的喜好对其进行自定义。

我使用以下代码做了类似的事情。

 <?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"> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="400dp" android:background="@color/dark_grey"> <com.google.android.material.appbar.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:popupTheme="@style/Theme.AppCompat.NoActionBar" app:layout_collapseMode="parallax"> <ImageView android:layout_width="24dp" android:layout_height="24dp" android:contentDescription="TODO" android:scaleType="centerCrop" android:src="@drawable/ic_arrow_left_vector" /> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right" android:layout_marginEnd="12dp" android:contentDescription="TODO" android:src="@mipmap/ic_launcher" /> </androidx.appcompat.widget.Toolbar> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_begin="56dp" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_begin="359dp" /> <androidx.cardview.widget.CardView android:id="@+id/cv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="@+id/guideline2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/guideline"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="My CardView" android:textStyle="bold" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@mipmap/ic_launcher" /> </LinearLayout> </androidx.cardview.widget.CardView> <LinearLayout android:id="@+id/ll_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/cv_1"> <TextView android:id="@+id/tv_1" android:layout_width="145dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginStart="24dp" android:text="content 1" android:textColor="@color/md_red" android:textSize="24sp" android:textStyle="bold" /> <TextView android:id="@+id/tv_2" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginStart="24dp" android:text="Content 2" android:textColor="@color/md_red" android:textSize="24sp" android:textStyle="bold" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottom_navigation_bar" android:layout_below="@+id/tab_bar_main" app:layout_behavior="@string/appbar_scrolling_view_behavior"> </androidx.viewpager.widget.ViewPager> </androidx.coordinatorlayout.widget.CoordinatorLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_bar_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginEnd="24dp"> <com.google.android.material.tabs.TabItem android:id="@+id/ti_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Menu 1" /> <com.google.android.material.tabs.TabItem android:id="@+id/ti_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Menu 2" /> <com.google.android.material.tabs.TabItem android:id="@+id/ti_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Menu 3" /> <com.google.android.material.tabs.TabItem android:id="@+id/ti_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Menu 4" /> </com.google.android.material.tabs.TabLayout> </RelativeLayout>

您可以做的是使用协调器布局并在内部使用带有滚动视图的折叠工具栏,并在滚动视图中将 setNestedScrollingEnabled 设置为 true。 这样,滚动视图响应用户滚动,并在滚动视图完全向上滚动时折叠。

而且你在错误的 position 中有结束标签,我会在这里解决它们。

<LinearLayout 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:background="#e6e6e6"
android:orientation="vertical">


<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/PostViewerToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:title="Post View" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:nestedScrollingEnabled="true"
    tools:ignore="ScrollViewCount">

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

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/SharedImg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:adjustViewBounds="true" />

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="10dp"
                android:background="#f1f1f1" />

            <TextView
                android:id="@+id/DescriptionTv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Post description" />
        </androidx.cardview.widget.CardView>

    </LinearLayout>
</ScrollView>

<androidx.viewpager.widget.ViewPager
    android:id="@+id/Viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="5dp">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/InvolveTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</androidx.viewpager.widget.ViewPager>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM