简体   繁体   English

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

[英]Part of fragment inside ViewPager getting cut off at bottom of screen(Android)

I have defined a basic Coordinator layout for my view: 我为我的视图定义了一个基本的协调器布局:

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

There are multiple tabs in my view pager. 我的视图寻呼机中有多个选项卡。 I am posting one of my simple fragments: 我发布了一个简单的片段:

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

What happens is that the FAB goes out of screen. 会发生什么是FAB离开屏幕。 It's just not the FAB, but other fragments with cardviews/listviews are getting cut off at the bottom of the screen. 这不是FAB,但其他具有cardviews / listview的片段在屏幕底部被截断。 I looked around for a solution, and came up with a hacky solution: Removing the property app:layout_scrollFlags="scroll|enterAlways" from the Toolbar . 我四处寻找解决方案,并提出了一个hacky解决方案: 从工具栏中删除属性app:layout_scrollFlags =“scroll | enterAlways”

I can't understand what might be causing this to happen, and how removing the above solved the problem. 我无法理解可能导致这种情况发生的原因,以及如何解决上述问题解决了这个问题。 Is it some bug in the support library? 它是支持库中的一些错误吗? Is there a better way to solve this? 有没有更好的方法来解决这个问题? Another solution I had come across is from here - to keep the FAB button directly under a Coordinator layout in the activity, and make it visible only in the fragment you need. 我遇到的另一个解决方案是从这里开始 - 将FAB按钮直接保持在活动中的Coordinator布局下,并使其仅在您需要的片段中可见。 Did not seem like a good solution to me. 对我来说似乎不是一个好的解决方案。 Also, other views in my fragments were also getting cut off. 此外,我片段中的其他观点也被切断了。

我已经通过更改工具栏app:layout_scrollFlags="scroll|enterAlways"修复了这个问题app:layout_scrollFlags="scroll|enterAlways"属性只是app:layout_scrollFlags="enterAlways"

I have a layout similar to yours: activity with a toolbar going off the top and tabs. 我有一个类似于你的布局:带有工具栏的活动从顶部和标签开始。 And the requirement for fragments is to have their own independent FABs. 片段的要求是拥有自己独立的FAB。

I couldn't cope with it directly in the xml, so then I nailed it that way: 我无法直接在xml中处理它,所以我就这样钉了它:

Activity layout 活动布局

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

Fragment layout: 片段布局:

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

And a bit of code to make the FAB stay where it intended to be. 还有一些代码可以让FAB保持原样。 Fragment onCreateView: 片段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;
    }

This is because you are using CoordinatorLayout with ListView . 这是因为您正在使用带有ListView CoordinatorLayout You can change your implementation to RecyclerView to achieve correct scroll. 您可以将实现更改为RecyclerView以实现正确的滚动。

check my answer here . 在这里查看我的答案。 This may help you. 这可能对你有所帮助。

Remove scroll from the toolbar layout_scrollFlags attribute. 从工具栏layout_scrollFlags属性中删除滚动。 You should have something like this: app:layout_scrollFlags="enterAlways|enterAlwaysCollapsed" 你应该有这样的东西: app:layout_scrollFlags="enterAlways|enterAlwaysCollapsed"

There are 2 solutions to this problem: 这个问题有两个解决方案:

  • You can remove Toolbar's scroll from app:layout_scrollFlags (eg app:layout_scrollFlags="enterAlways" ) or remove entire app:layout_scrollFlags 您可以从app:layout_scrollFlags删除工具栏的scroll app:layout_scrollFlags (例如app:layout_scrollFlags="enterAlways" )或删除整个app:layout_scrollFlags
  • If you require the scroll attribute, the main layout content/fragment must use RecyclerView or NestedScrollView . 如果需要scroll属性,则主布局内容/片段必须使用RecyclerViewNestedScrollView

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

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

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