【问题标题】:viewpager parallax scrolling and collapse toolbar on scrollviewpager 视差滚动和滚动时折叠工具栏
【发布时间】:2016-03-09 08:26:43
【问题描述】:

参考图片,我想在 viewpager 中创建视差滚动效果。 ImageView 和 webview 是视图分页器片段的布局,而工具栏位于包含视图分页器的 Activity 上。

我的视差滚动效果正常工作,但下面的显示和隐藏工具栏不是我的活动和片段代码。

main.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/htab_maincontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/htab_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/htab_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:minHeight="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/htab_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

片段.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:id="@+id/htab_maincontent"
android:layout_height="match_parent"
android:layout_width="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/htab_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/htab_collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/htab_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:src="@drawable/tes"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"
    android:clipToPadding="false"
    android:isScrollContainer="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!--<TextView
        android:id="@+id/pagename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />-->

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

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></WebView>
    </LinearLayout>


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

所以我的问题是如何使活动中的工具栏获得片段中的滚动行为并在用户上下滚动时显示或隐藏

【问题讨论】:

  • 尝试从 viewpager 中删除 app:layout_behavior="@string/appbar_scrolling_view_behavior"
  • 我尝试了但没有任何其他建议?
  • 兄弟我更新了我的答案,请检查它
  • 兄弟你试过我更新的答案吗?

标签: android android-layout material-design coordinator-layout


【解决方案1】:

您必须按照以下方式进行操作

    <CoordinatorLayout>
       <AppBarLayout>
         <Toolbar >
           <ImageView      
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:minHeight="100dp"/>
        </Toolbar>
      </AppBarLayout>
    </CoordinatorLayout>

也可以参考这个链接https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

【讨论】:

  • 我的图像视图位于片段内,我的视图寻呼机中的每个页面都有不同的图像。有什么方法可以将图像视图保留在片段中并达到我想要的效果?
  • 你可以有一个带有视差标志的框架布局,你可以设置片段并在代码中动态创建图像视图
  • 很抱歉没有真正理解你的意思。为什么我需要在片段中有一个带有视差标志的框架布局并动态创建图像视图?我在片段中的图像视图的视差滚动效果工作正常。我的问题是我无法在我的活动中隐藏工具栏,以便在滚动内容时向用户提供我的内容的全屏视图
【解决方案2】:

试试这个

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v4.view.ViewPager
        android:id="@+id/htab_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>

希望对你有帮助

【讨论】:

  • 你试过了吗?它对你有用吗?我试过了,它不适合我
  • 在您编辑的答案中意味着我必须用另一个nestedscrollview 包装我的viewpager?我尝试了所有我的 viewpager 内容都被 nestedscrollview 覆盖了我看不到 viewpager 内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 2017-09-27
相关资源
最近更新 更多