如何修复“ ViewPager选项卡之间的水平滑动”?

时间:2019-01-24 13:38:16

标签: java android android-recyclerview android-viewpager android-collapsingtoolbarlayout

我在两个标签中都有一个RecyclerView

问题是:当我滚动到列表顶部,然后尝试水平滑动到下一个选项卡时,有一段时间没有水平移动了。 “只有在滚动到列表的顶部或底部时才会发生这种情况”。

提示:我正在使用CollapsingToolbarLayout

我试图将setNestedScrollingEnabled()设为(假)。 这样可以解决水平滑动的问题,但可以避免折叠。

enter image description here

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
......>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/my_coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">


        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:expandedTitleGravity="top"
                app:layout_scrollFlags="scroll|enterAlways">


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:contentInsetStartWithNavigation="0dp"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:contentDescription="@null"
                        android:src="@drawable/dictionary" />
                </android.support.v7.widget.Toolbar>

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

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:tabGravity="fill"
                app:tabIndicatorHeight="3dp"
                app:tabMode="fixed">

                     <! Two tabs here >

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

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

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

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

    <android.support.design.widget.NavigationView
    ....../>
</android.support.v4.widget.DrawerLayout>

Java代码段:

    mRecyclerView = mRootView.findViewById(R.id.list);
    mRecyclerView.setHasFixedSize(true);

    // For linear displaying.
    mLayoutManager = new LinearLayoutManager(mRootView.getContext());
    mRecyclerView.setNestedScrollingEnabled(true);
    mRecyclerView.setLayoutManager(mLayoutManager);

    WordsAdapter mAdapter = new WordsAdapter(mWords, this);
    mRecyclerView.setAdapter(mAdapter);

2 个答案:

答案 0 :(得分:0)

您应该签出detecting gestures。您的问题是由RecyclerView拦截了轻微垂直移动的触摸事件引起的。如果要使手指完全水平移动,则可以在两个选项卡之间切换,但是正常的滑动动作会带有一些垂直分量。要解决您的问题,可以在您的RecyclerView上添加一个onTouchListener,在其中您仅拦截大多数垂直事件。这样,水平滑动将传递到下面的TabView。

答案 1 :(得分:0)

只需添加以下内容即可解决此问题: viewPager.requestDisallowInterceptTouchEvent(false);

Java代码段:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    viewPager.requestDisallowInterceptTouchEvent(false);
    return super.dispatchTouchEvent(ev);
}
相关问题