【问题标题】:scrollview inside viewpager not scrollingviewpager 内的滚动视图不滚动
【发布时间】:2013-07-25 11:56:47
【问题描述】:

我在带有水平可滑动页面的 viewpager 中有一个小的垂直滚动视图。 问题是目前我无法通过在滚动视图上水平滑动来滚动页面。 我希望能够在滚动视图上水平滑动以滚动浏览 viewpager 的页面,并且还具有垂直滑动以滚动滚动视图内容的功能。

【问题讨论】:

    标签: android android-viewpager scrollview swipe


    【解决方案1】:

    您可以使用以下自定义滚动视图来做到这一点

    public class CustomScrollView extends ScrollView {
        private GestureDetector mGestureDetector;
    
        public CustomScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mGestureDetector = new GestureDetector(context, new YScrollDetector());
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
        }
    
        // Return false if we're scrolling in the x direction  
        class YScrollDetector extends SimpleOnGestureListener {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                return Math.abs(distanceY) > Math.abs(distanceX);
            }
        }
    }
    

    【讨论】:

    • 我试过了,但没有任何区别。水平 ViewPager 包含 2 个按钮和一个滚动视图。如果我在按钮上水平滑动,viewpager 页面会滚动,但如果我在滚动视图上执行此操作,则什么也不会发生,但我仍然可以滚动滚动视图
    【解决方案2】:

    我不知道是不是同样的情况,但我遇到了你描述的类似问题。我到处寻找这个答案:https://stackoverflow.com/a/35180360/1969966

    基本上,如果您的 Activity 结构类似于上面的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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">
    
        <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"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:background="?attr/colorPrimary"
                android:elevation="6dp"
                android:minHeight="?attr/actionBarSize"
                app:tabMode="fixed"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    
            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar"
                tools:context="net.voidynullness.android.tabitytabs.TabLayoutActivity"
                                               android:background="#ffffff"/>
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_info"/>
    </android.support.design.widget.CoordinatorLayout>
    

    更改为(查看“android.support.design.widget.AppBarLayout”):

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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">
    
        <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"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:background="?attr/colorPrimary"
                android:elevation="6dp"
                android:minHeight="?attr/actionBarSize"
                app:tabMode="fixed"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
        </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar"
                tools:context="net.voidynullness.android.tabitytabs.TabLayoutActivity"
                                               android:background="#ffffff"/>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_info"/>
    </android.support.design.widget.CoordinatorLayout>
    

    现在,您可以水平和垂直滑动!

    【讨论】:

    • 你的意思是横向和纵向?
    • 抱歉@bwegs,已修复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多