android - 使用 NestedScrollView 内的 Viewpager 折叠工具栏布局

标签 android android-viewpager android-collapsingtoolbarlayout

我在嵌套的 Scrollview 中使用带有 NestedScrollView 和 Viewpager 的折叠工具栏布局。

我有 3 个选项卡,这些选项卡有 3 个 fragment 。这些 fragment 正在使用 RecyclerView 设置数据。

现在使用 nestedScrollView 和 viewpager,当我滚动 RecyclerView 内容时,折叠效果无法使用。

我还需要放置 NestedScrollView,因为我有一些信息需要显示在选项卡上方。

这是我的代码:

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

    <android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginEnd="64dp"
    app:expandedTitleMarginStart="48dp"
    app:expandedTitleTextAppearance="@style/TransparentText">

    <FrameLayout
        android:id="@+id/carouselLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="parallax">

        <ImageView
            android:id="@+id/coverImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/example"
            android:scaleType="centerCrop"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:gravity="bottom"
            android:orientation="vertical"
            android:layout_gravity="bottom"
            android:padding="@dimen/profile_image_margin"
            android:background="@drawable/gradient_bg"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_height="wrap_content">

            <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/profile_image_margin"
            android:textSize="@dimen/text_size_xlarge"
            android:textStyle="bold"
            android:textColor="@color/white"
            android:text="Title"
            android:id="@+id/content_title"/>

        </LinearLayout>

    </FrameLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:theme="@style/ActionBarThemeOverlay"
            app:popupTheme="@style/ActionBarPopupThemeOverlay"
            app:layout_scrollFlags="scroll|enterAlways"
            android:background="@drawable/gradient_bg" />

    </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"
    android:isScrollContainer="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="sdjsfksdfsd"
            android:textColor="@color/red"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="csdffsfsdfsdfsdf"
            android:textColor="@color/red"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/slidingTabs"
            android:layout_width="match_parent"
            app:tabMode="scrollable"
            app:tabGravity="fill"
            android:layout_height="wrap_content"/>

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

        </android.support.v4.view.ViewPager>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

如果您知道如何在 android 中的 NestedScrollview 中实现 Recyclerview,请帮助我,以便我可以正常工作。

最佳答案

这似乎已经在这里得到了回答: https://stackoverflow.com/a/31561790/4931825

您还可以查看上述答案中提供的示例 https://github.com/TheLittleNaruto/SupportDesignExample/

作者创建了一个扩展 NestedScrollView 的自定义 NestedScrollView 类,它覆盖了 onInterceptTouchEvent 方法,如下所示:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final float x = ev.getX();
    final float y = ev.getY();
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if (xDistance > yDistance)
                return false;
    }

    return super.onInterceptTouchEvent(ev) || ev.getPointerCount() == 2;
}

和一个扩展 ViewPager 的自定义 ViewPager 类覆盖了它的 onMeasure 方法,如下所示:

    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)     {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int height = 0;
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if (h > height) height = h;
    }
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

最后,他在 CoordinatorLayout 中使用了两者:

 <com.thelittlenaruto.supportdesignexample.customview.MyNestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="visible">

        <com.thelittlenaruto.supportdesignexample.customview.WrapContentHeightViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</com.thelittlenaruto.supportdesignexample.customview.MyNestedScrollView>

关于android - 使用 NestedScrollView 内的 Viewpager 折叠工具栏布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31958749/

相关文章:

android - 为什么不能在 Fragment 中使用 ViewPager?它实际上是

android - 如何使用 ViewPager 的 fragment 首选项?

android - Design v24.2.1 CollapsingToolbarLayout 中的标题问题

android - 带 fragment 的 Tabhost 重新创建 View

android - AppBarLayout 禁用选定 View 上的拖动

android - 使用 CollapsingToolbarLayout 时,AppBar 不会与状态栏重叠

android - 如何使用 TrafficStats.incrementOperationCount()?

Android,如何捕获神秘的系统异常

android - 重新启动加载程序调用时出错

android - 从android中的地址获取经纬度