【问题标题】:How to swipe ViewPager while the content is being scrolled?滚动内容时如何滑动 ViewPager?
【发布时间】:2018-05-01 06:03:51
【问题描述】:

我可以很好地实现TabLayoutViewPager,但是当滚动ViewPager 中的内容(减速动画)时,我无法左右滑动以更改为ViewPager 中的其他片段。我必须等到滚动动画停止后再滑动。

下面是我的一些代码sn-ps。

我有一个简单的activity_main.xml 布局,如下所示。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarHome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"/>

    <LinearLayout
        android:id="@+id/mainContainerLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">
    </LinearLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/btmNavView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        app:menu="@menu/menu_main" />

</LinearLayout>

然后我将包含TabLayoutViewPager 的片段膨胀到activity_main.xml 中间的LinearLayout。此布局如下所示:

<LinearLayout 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:orientation="vertical">

    <com.pchatanan.sontana.custom_views.AppTabLayout
        android:id="@+id/contactTabLayout"
        app:tabMode="fixed"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        style="@style/AppTabLayout"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/contactViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我的片段内部的逻辑如下:

fragmentArray = new Fragment[]{new SimpleFragment(), new SimpleFragment(), new SimpleFragment()};
titleArray = new String[]{"fragment1", "fragment2", "fragment3"};

contactPagerAdapter = new AppPagerAdapter(getChildFragmentManager(), fragmentArray, titleArray);
contactViewPager.setAdapter(contactPagerAdapter);
contactViewPager.setOffscreenPageLimit(fragmentArray.length - 1);
contactTabLayout.setupWithViewPager(contactViewPager);

【问题讨论】:

  • 片段的内容是什么,是滚动视图还是回收器视图。
  • @Khemraj 这是一个简单的滚动视图。

标签: android android-fragments android-viewpager android-tablayout


【解决方案1】:

使用这个而不是默认的 ViewPager。 检测ScrollViewRecyclerView 何时滚动。然后调用

viewPager.setPagingEnabled(pagingEnable);

如果启用分页

public class CustomViewPager extends ViewPager {
    private boolean isPagingEnabled = true;

    public CustomViewPager(Context context) {
        super(context);
    }

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

    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

更新:

ViewPager 滑动时,您可以反向操作以禁用ScrollView 滚动

mScrollView.requestDisallowInterceptTouchEvent(true);

【讨论】:

  • 这个自定义类允许禁用滑动,但就我而言,我认为不需要禁用滑动。由于默认的 ViewPager 已经默认启用滑动,这个类将如何解决我的问题?
  • 问题是两个触摸被拦截,一个是View pager,第二个是ScrollView,所以你可以在触摸另一个时禁用一个。也请参阅更新的答案。
  • ScrollView 和 ViewPager 位于不同的分片中,如何处理它们之间的通信?
  • 感谢您的建议。现在我让它工作了,并将在这里发布答案。和你的有点不一样。
  • 如果我有时间会发布完整的答案。
【解决方案2】:

类似于@Khemraj 的建议,问题是 ViewPager 没有拦截到触摸。它只被 ScrollView 拦截。为了解决这个问题,我们应该允许 ViewPage 在滚动时拦截触摸:

scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
    @Override
    public void onScrollChange(View view, int i, int i1, int i2, int i3) {
        viewPager.requestDisallowInterceptTouchEvent(false);
    }
});

更好的实现是在包含滚动视图的片段上使用 CallBackListener。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多