【问题标题】:Viewpager is being pushed out of the screen [CoordinatorLayout] [Design Library]Viewpager 被推出屏幕 [CoordinatorLayout] [Design Library]
【发布时间】:2016-05-23 08:11:35
【问题描述】:

我有一个包含 3 个片段的 viewpager 的简单应用程序。在其中一个片段中,我有 Recyclerview 列表。向下滚动时,工具栏折叠,向上滚动时显示。我的问题是,当工具栏折叠并且我向左/向右滚动时,我正在以编程方式扩展它,它会向下推动 viewpager,而不是仅仅重叠它。这导致屏幕视图的不令人满意的位移。如何让工具栏与我的查看器重叠,而不是在展开时将其向下推? 我对问题做了一个简短的记录click

这是我的 main_layout:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="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:fitsSystemWindows="true"
    tools:context=".UI.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/appBarLayouy"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tabsHeight"
            style="@style/NavigationTab"/>


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

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


    <include layout="@layout/content_main"/>

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/floatingActionMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        fab:fab_addButtonColorNormal="@color/blood_orange"
        fab:fab_addButtonColorPressed="@color/dirtyWhite"
        fab:fab_addButtonPlusIconColor="@color/dirtyWhite"
        fab:fab_addButtonSize = "normal"
        fab:fab_labelStyle="@style/menu_labels_style"
        fab:fab_labelsPosition="left"
        app:layout_anchor="@id/viewpager"
        app:layout_anchorGravity="bottom|end">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/createPlanBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/blood_orange"
            fab:fab_title="Create a plan"
            fab:fab_size="normal"
            app:fab_icon="@drawable/ic_event_white_48dp"
            fab:fab_colorPressed="@color/dirtyWhite"/>

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/changeStatusBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/blood_orange"
            fab:fab_size="normal"
            app:fab_icon="@drawable/ic_textsms_white_48dp"
            fab:fab_title="Change status"
            fab:fab_colorPressed="@color/dirtyWhite"/>

    </com.getbase.floatingactionbutton.FloatingActionsMenu>

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

这是我的包含 recyclerview 布局的片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
            android:background="@color/tab_bg">

<android.support.v7.widget.RecyclerView
    android:id="@+id/feedCardViewList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="true"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

</RelativeLayout>

content_main 布局:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".UI.MainActivity"
tools:showIn="@layout/activity_main"
app:layout_behavior="@string/appbar_scrolling_view_behavior">


</RelativeLayout>

【问题讨论】:

  • 如果你删除 app:layout_scrollFlags="scroll|enterAlways" 会发生这种情况吗?
  • @GeorgiKoemdzhiev - content_main 的内容是什么?因为你有一个ViewPagerViewPager 下的一个布局。
  • @GeorgiKoemdzhiev - 我认为这是因为 SupportLibrary,目前还没有官方答案!
  • @GeorgiKoemdzhiev - 我可以为您做到这一点(通过创建一个新问题),但是,当您指定 ViewPager 时,这意味着您还需要通过创建新闻布局来添加片段,这是另一个问题,我想你需要删除 Include,无论如何,我认为这是因为 CoordinatorLayout,我认为我们应该联系谷歌员工。
  • 这是最好的官方网站 - code.google.com/p/android/issues/…

标签: android android-fragments android-viewpager android-recyclerview android-coordinatorlayout


【解决方案1】:

今天遇到了这个问题。我不确定我是否以某种方式遗漏了某些东西并错误地实现了 CoordinatorLayout,或者它是否是一个错误。但如果有人仍然遇到这个问题,我通过根据应用栏的高度调整工具栏下方内容的边距以编程方式解决了这个问题。这里是:

布局 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:layout_width="match_parent"
android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            ...collapsing toolbar content...

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

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

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        ...content beneath toolbar...

    </FrameLayout>

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

代码(膨胀布局后):

AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
FrameLayout contentLayout = (FrameLayout) view.findViewById(R.id.content);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) contentLayout.getLayoutParams();
        layoutParams.setMargins(0, 0, 0, appBarLayout.getMeasuredHeight() + verticalOffset);
        contentLayout.requestLayout();
    }

});

【讨论】:

  • 感谢您分享您的答案!我还没有测试过,但我会接受你的回答,因为它是迄今为止最详细的回答,它也可以帮助其他人。
【解决方案2】:

this 可能重复

您可以尝试将工具栏app:layout_scrollFlags="scroll|enterAlways" 属性更改为app:layout_scrollFlags="enterAlways"

【讨论】:

  • 这个为我修好了。点赞^^
  • 完美修复。谢谢
【解决方案3】:

我有同样的问题,所以我暂时放了一个 55dp 底部边距 android:layout_marginBottom="55dp" 看起来不错,我希望这个问题能尽快解决。

【讨论】:

    【解决方案4】:

    大家

    我也遇到了同样的问题。

    这个问题可以通过简单的移除来轻松解决

    app:layout_scrollFlags="scroll|enterAlways"

    来自 xmlfile 中的工具条码。

    我也遇到了同样的问题,但删除后不久 viewpager 在屏幕内自行排列(没有超出屏幕限制)

    这是默认代码

     <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">
    

    把这个改成

      <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
    
            app:popupTheme="@style/AppTheme.PopupOverlay">
    

    倒数第二行。

    【讨论】:

    • 这会产生一个单独的问题
    【解决方案5】:

    尝试在您的 viewpager 标记中提供Android:MarginBottom = "5dp",在这里您使用层高度作为“Match_Parent”,因此它覆盖了底部区域。

    【讨论】:

    • 这是来自 SupportLibrary 的默认配置或默认设计,因此,OP 不需要更改任何内容。
    • 我已经试过了,这是解决问题的方法,它只会破坏我的布局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多