ViewPager里面的部分片段在屏幕底部被截断(Android)

时间:2015-12-22 01:15:03

标签: android android-layout android-fragments material-design android-support-library

我为视图定义了一个基本的协调器布局:

<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/white"
            app:tabMode="fixed"/>
    </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" />

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

我的视图寻呼机中有多个标签。我发布了一个简单的片段:

<ListView
    android:id="@+id/transferList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_add_white" />

FAB离开屏幕会发生什么。它不是FAB,但其他具有cardviews / listview的片段在屏幕底部被截断。我四处寻找解决方案,并提出了一个hacky解决方案:删除属性app:layout_scrollFlags =&#34;滚动| enterAlways&#34;来自工具栏

我无法理解可能导致这种情况发生的原因,以及如何删除上述内容解决了问题。它是支持库中的一些错误吗?有没有更好的方法来解决这个问题?我遇到的另一个解决方案来自here - 将FAB按钮直接保存在活动中的Coordinator布局下,并使其仅在您需要的片段中可见。对我来说似乎不是一个好的解决方案。此外,我片段中的其他观点也被切断了。

5 个答案:

答案 0 :(得分:24)

我已修复此问题,方法是将工具栏app:layout_scrollFlags="scroll|enterAlways"属性更改为app:layout_scrollFlags="enterAlways"

答案 1 :(得分:4)

我有一个类似于你的布局:带有工具栏的活动从顶部和标签开始。片段的要求是拥有自己独立的FAB。

我无法直接在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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/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/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginBottom="20dp"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabContentStart="72dp"
        app:tabMode="scrollable" />

</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" />

片段布局:

<FrameLayout 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">

<include
    android:id="@+id/scroll"
    layout="@layout/content_activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right|end"
    android:src="@android:drawable/ic_input_add"
    app:borderWidth="0dp"
    app:fabSize="mini"

    app:useCompatPadding="true" />

还有一些代码可以让FAB保持原样。 碎片onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_tabs2, container, false);
        final FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
        final TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
        final AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int maxAbsOffset = appBarLayout.getMeasuredHeight() - tabLayout.getMeasuredHeight();
                fab.setTranslationY(-maxAbsOffset - verticalOffset);
            }

        });

        return rootView;
    }

答案 2 :(得分:1)

这是因为您将CoordinatorLayoutListView一起使用。您可以将实施更改为RecyclerView以实现正确的滚动。

检查我的回答here。这可能会对你有帮助。

答案 3 :(得分:0)

从工具栏layout_scrollFlags属性中删除滚动。你应该有这样的东西: {{1}}

答案 4 :(得分:0)

有2种解决方案:

  • 您可以从scroll中删除工具栏的app:layout_scrollFlags(例如app:layout_scrollFlags="enterAlways"),也可以删除整个app:layout_scrollFlags
  • 如果您需要scroll属性,则主布局内容/片段必须使用RecyclerViewNestedScrollView

https://code.luasoftware.com/tutorials/android/bottom-of-fragment-in-viewpager-out-of-screen/