Android ViewPager内部CoordinatorLayout中的非滚动片段

Android ViewPager内部CoordinatorLayout中的非滚动片段,android,android-viewpager,android-design-library,android-coordinatorlayout,Android,Android Viewpager,Android Design Library,Android Coordinatorlayout,我正在活动中的CoordinatorLayout(来自最新版本的Design Library)中使用ViewPager。 此ViewPager的某些片段具有诸如RecyclerView或NestedScrollView之类的布局,但有些片段由于内容较小而无法滚动 <android.support.design.widget.AppBarLayout android:id="@+id/tabanim_appbar" android:layout_wid

我正在活动中的CoordinatorLayout(来自最新版本的Design Library)中使用ViewPager。 此ViewPager的某些片段具有诸如RecyclerView或NestedScrollView之类的布局,但有些片段由于内容较小而无法滚动

    <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作为根视图的片段中,我需要有一个锚定到底部的按钮,但它似乎是从屏幕上画出来的。 为了能够看到它,我需要添加一个与工具栏高度相等的底部填充

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

同样,元素上设置为“中心”的布局重力似乎不在该碎片可见区域的中心

我的理解是CoordinatorLayout仅用于滚动内容,对吗? 这样,仅使用常规视图组(如FrameLayout、RelativeLayout、LinearLayout)来显示ViewPager片段,就可以将其底部从屏幕上画出来


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

该问题是由父容器从ViewPager截取触摸事件引起的。父滚动容器侦听滚动触摸事件,如果存在垂直移位,它将从其子视图中窃取事件。它只是认为用户希望垂直滚动父容器

下面是解决它的代码

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    private GestureDetector mGestureDetector;
    private View.OnTouchListener mGestureListener;

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return Math.abs(distanceY) > Math.abs(distanceX);
        }
    }

}
使用此类而不是您的ViewPager

   <(your packagename).CustomScrollView
    android:id="@+id/tabanim_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/> 

参考下面的内容对我很有用

扩展滚动视图行为

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;
}
}
指定要查看寻呼机的

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

你找到解决办法了吗?@cdodo你找到了解决办法吗?我找到了解决办法。检查下面的答案
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) viewPager.getLayoutParams();
    MyBehavior behavior = (MyBehavior) lp.getBehavior();
    behavior.setLayout(viewPager);