【问题标题】:Non-Scrolling Fragment in a ViewPager inside CoordinatorLayoutCoordinatorLayout 内的 ViewPager 中的非滚动片段
【发布时间】:2015-10-13 11:12:30
【问题描述】:

我在 Activity 的 CoordinatorLayout(来自最新版本的设计库)中使用 ViewPager。 此 ViewPager 的某些 Fragment 具有 RecyclerView 或 NestedScrollView 等布局,但由于内容太小,某些 Fragment 无法滚动。

    <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 作为根视图的 Fragment 中,我需要一个固定在底部的按钮,但它似乎是在屏幕外绘制的。 为了能够看到它,我需要添加一个等于工具栏高度的底部填充。

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

同样,在元素上设置为“center”的 layout_gravity 似乎不在此片段的可见区域的中心。

我的理解是 CoordinatorLayout 仅用于处理滚动内容,对吗? 这样 ViewPager 片段只使用常规的 ViewGroup,如 FrameLayout、RelativeLayout、LinearLayout 将其底部绘制到屏幕外?

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

【问题讨论】:

  • 你有没有为此找到解决方案?
  • @cdodo 你找到解决办法了吗?
  • 我找到了解决方案。检查下面的答案

标签: android android-viewpager android-design-library android-coordinatorlayout


【解决方案1】:

参考this answer 以下内容对我有用

扩展滚动视图行为

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>

在 onCreate 的活动中

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

【讨论】:

    【解决方案2】:

    这个问题是由于父容器从 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"/> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2023-04-08
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多