android - ViewPager 中的第二个 fragment 使用滚动事件

标签 android android-layout android-fragments android-viewpager android-recyclerview

我有一个 SwipeRefreshLayout 在我的 Activity 中托管 ViewPager。 我正在向 ViewPager 添加两个 fragment 。第一个 fragment 有 RecyclerView,第二个 fragment 有一个 ScrollView

在第一个 fragment 上,SwipeRefreshLayout 正在消耗滚动事件。我添加了 OnScrollListener,如果 View 不在顶部,我将禁用刷新。

我在第二个 fragment 中遇到了与 ScrollView 相同的问题,我将 ViewTreeObserver.OnScrollChangedListener() 添加到 ScrollView

但是,当我在第一页时,滚动事件会传递到第二页。

我试图在第一个 fragment 的父 View 上设置 android:clickable="true" 但它不起作用。

任何建议都会有所帮助。

我的 Activity .xml

<include android:id="@+id/toolbar" layout="@layout/include_toolbar" />

<FrameLayout
    android:id="@id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/login_background"
            android:fillViewport="true"
            android:fitsSystemWindows="true"
            android:layout_below="@id/tab_layout">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/last_update"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/normal_padding"
                    android:paddingTop="@dimen/small_padding"
                    android:textColor="@color/gray"
                    android:paddingBottom="@dimen/small_padding"
                    android:textSize="@dimen/caption"
                    android:textStyle="bold" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/pager"
                    android:layout_below="@id/last_update"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"/>

            </RelativeLayout>

        </android.support.v4.widget.SwipeRefreshLayout>
    </RelativeLayout>
</FrameLayout>

first_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="16dp"
android:background="@android:color/transparent"
android:clickable="true">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />
</LinearLayout>

second_fragment.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">

There are some child views here. 

</ScrollView

第二个 fragment

    onScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_container);

            int scrollY = scrollView.getScrollY();
            if (scrollY == 0) swipeRefreshLayout.setEnabled(true);
            else swipeRefreshLayout.setEnabled(false);

        }
    };

    scrollView.getViewTreeObserver().addOnScrollChangedListener(onScrollChangedListener);

最佳答案

这是因为第二个 Fragment 是与第一个 Fragment 一起创建的,并且它获得了焦点。我在使用 Depth Page Transformer 时遇到了这个问题。第二个 Fragment 在第一个 Fragment 下渲染,因此消耗了触摸。

尝试使用以下页面转换器:

viewPager.setPageTransformer(false, new CustomViewPager.PageTransformer() {

    @Override
    public void transformPage(View view, float position) {
        // TODO Auto-generated method stub

        int pageWidth = view.getWidth();
        int pageHeight=view.getHeight();
        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);
        } 
        else if (position <= 1) { // [-1,1]

         float scaleFactor = Math.max(min_scale, 1 -Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                view.setTranslationX(horzMargin - vertMargin / 2);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / 2);
            }

            // Scale the page down (between MIN_SCALE and 1)
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

            // Fade the page relative to its size.
            view.setAlpha((float) (0.2 +
                    (scaleFactor - min_scale) /
                    (1 - min_scale) * (1 - 0.2)));

        }
        else { 
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }       
});

第二个 Fragment 现在呈现在右侧,因此触摸事件被传递给第一个 Fragment。

关于android - ViewPager 中的第二个 fragment 使用滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33635907/

相关文章:

android - 如何使用 Espresso 为 RecyclerView 中的项目设置 IdlingResource?

android自定义 toast 持续时间

android - 如何检查我的用户设备上是否安装了 Android 电子市场?

android-layout - Android 资源使用 XHDPI、HDPI

android - 遍历Fragment返回栈时如何刷新ActionBar导航项?

java - 选择要在 Activity 中显示的特定 Fragment?

php - Android与mysql数据库的连接

android - 在 Android 库项目中替换(或 "Override")字符串

android - ViewPager/Fragment 在底部重叠 LinearLayout

Android自定义FragmentPagerAdapter的外观