非滚动片段在ViewPager内CoordinatorLayout [英] Non-Scrolling Fragment in a ViewPager inside CoordinatorLayout

查看:704
本文介绍了非滚动片段在ViewPager内CoordinatorLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在活动使用ViewPager在CoordinatorLayout(从设计库的最新版本)。 这ViewPager一些片段的布局,如RecyclerView或NestedScrollView,但有些只是不能滚动给予他们的小内容。

I am using a ViewPager in a CoordinatorLayout (from latest version of Design Library) in an Activity. Some fragments for this ViewPager have layouts such as RecyclerView or NestedScrollView, but some just cannot scroll given their small content.

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/tabanim_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/tabanim_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

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

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

但是,在一个片段用的FrameLayout作为根视图,我需要有被固定在底部的一个按钮,但它似乎是离屏描绘。 为了能够看到它,我需要添加一个底部填充等于工具栏的高度。

But in one Fragment with a FrameLayout as the root view, I need to have a button that is anchored to the bottom, but it appears to be drawn off-screen. To be able to see it, I need to add a bottom padding equals to the height of the Toolbar.

  <FrameLayout 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:background="@android:color/white">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Home screen"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_gravity="bottom"
        android:gravity="center"
        android:text="@string/brand"
        android:textColor="@color/brandColor"
        android:textSize="20sp" />
</FrameLayout>

同样地,layout_gravity设置为中心的元件上不出现是在可见光区域的中心为这样的片段。

Likewise, a layout_gravity set to 'center' on an element does not appear to be in the center of the visible area for this fragment.

我的理解是,CoordinatorLayout只intented与滚动的内容合作,是正确的? 因此仅仅使用普通的ViewGroup如的FrameLayout,RelativeLayout的,LinearLayout中的ViewPager碎片将有自己的底部排出屏?

My understanding is that CoordinatorLayout is only intented to work with scrolling contents, is that correct ? So that using only regular ViewGroup such as FrameLayout, RelativeLayout, LinearLayout for the ViewPager fragments will have their bottom part drawn off-screen ?

在这种情况下,我需要从该片段布局中删除该按钮,将其移动到包含CoordinatorLayout活动的布局?它需要仅显示在第一个片段上。

In that case, do I need to remove this button from this fragment layout and move it to the activity layout containing the CoordinatorLayout ? It needs to be shown only on the first fragment.

推荐答案

从参考这个答案下面的工作了我

扩展滚动型行为

public class MyBehavior extends AppBarLayout.ScrollingViewBehavior {

private View layout;

public MyBehavior() {
}

public MyBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    boolean result = super.onDependentViewChanged(parent, child, dependency);
    if (layout != null) {
        layout.setPadding(layout.getPaddingLeft(), layout.getPaddingTop(), layout
                .getPaddingRight(), layout.getTop());
    }
    return result;
}

public void setLayout(View layout) {
    this.layout = layout;
}
}

指定要viewpager

Specify that to viewpager

<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.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.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.AppBarLayout>
<org.nsu.myapplication.VerticalViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="your.domain.name.MyBehavior"
    />
  </android.support.design.widget.CoordinatorLayout>

在活动的onCreate

and in onCreate of activity

CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) viewPager.getLayoutParams();
    MyBehavior behavior = (MyBehavior) lp.getBehavior();
    behavior.setLayout(viewPager);

这篇关于非滚动片段在ViewPager内CoordinatorLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆