java - The up and down sliding event in Fragment will be responded to the previous Fragment instead of the current one
某草草
某草草 2017-05-31 10:37:03
0
1
966

I am currently developing an Android app with only one activity. There is a ViewPager in the layout file, and its adapter has three Fragments tied to it. The layout files of the first two Fragments are the outermost SwipeRefreshLayout for pull-down refresh, and then a ScrollView is nested. The third one was also prepared to do the same but found a problem.

When I enter the app and display the first Fragment by default, sliding the screen up and down has a sliding effect. However, switching to the second Fragment and sliding has no effect. Then I discovered that when I slid in the second Fragment and then switched back to the first Fragment, I found that the first Fragment interface responded to my sliding operation. So I tried to switch to the third Fragment, and quickly switched to the second Fragment after sliding. Sure enough, the interface was sliding.

I don’t know what the reason is, but I tried a way: By overloading setUserVisibleHint(), once you leave a Fragment, directly set the entire Fragment to Invisible. In this way, it is indeed achieved The sliding operation is responded to by the current Fragment. But I still don’t understand why there was such a situation before - When sliding the screen in the first and second Fragment, the first Fragment responds, and when sliding the screen in the third Fragment , then the second Fragment responds to the sliding operation.

I want to know what went wrong, what caused it, and how can I solve it (without forcing it by setting Visibility)?

Fragment layout file code (only one is given, the other is similar):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_article_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:id="@+id/scroll_article_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <WebView
        android:id="@+id/articleWebView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:alpha="0"/>
        <ProgressBar
            android:id="@+id/webViewLoading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            style="@style/MyProgressBar"
            android:visibility="gone"/>
    </FrameLayout>
    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
某草草
某草草

reply all(1)
刘奇

Problem solved. .
There is a piece of information not described in the question. That is, in order to make the animation fade in and out instead of the default sliding when the ViewPager switches pages, I implemented an interface of the ViewPager class, ViewPager.PageTransformer. Then, I instantiate this class in Activity and execute mViewPager.setPageTransformer(true, pageTransformer); to set the switching animation to the animation I wrote myself. The problem lies in this animation. At the beginning, I implemented this interface like this:

import android.support.v4.view.ViewPager;
import android.view.View;

/*
*设置Fragment切换时的动画为淡入淡出
*/

public class NoSlidingPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_ALPHA = 0.0f;    //最小透明度

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();    //得到view宽

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left. 出了左边屏幕
            view.setAlpha(0);

        } else if (position <= 1) { // [-1,1]
            view.setTranslationX(-pageWidth * position);  //阻止页面的滑动
            float alphaFactor = Math.max(MIN_ALPHA, 1 - Math.abs(position));
            //透明度改变
            view.setAlpha(alphaFactor);
            if (alphaFactor == 0)
                view.setVisibility(View.INVISIBLE);     //页面不在当前界面显示,则使其Invisible,这句是为了解决Fragment对上下滑动事件监听的错乱,暂不知原因
            else if (view.getVisibility() == View.INVISIBLE)
                view.setVisibility(View.VISIBLE);       //页面在当前界面显示,则使其Visible,这句是为了解决Fragment对上下滑动事件监听的错乱,暂不知原因
        } else { // (1,+Infinity]
            // This page is way off-screen to the right.    出了右边屏幕
            view.setAlpha(0);
        }
    }

}

This piece of code is what I temporarily used to solve the "supernatural" phenomenon described in the question:

if (alphaFactor == 0)
    view.setVisibility(View.INVISIBLE);     //页面不在当前界面显示,则使其Invisible,这句是为了解决Fragment对上下滑动事件监听的错乱,暂不知原因
    else if (view.getVisibility() == View.INVISIBLE)
        view.setVisibility(View.VISIBLE);       //页面在当前界面显示,则使其Visible,这句是为了解决Fragment对上下滑动事件监听的错乱,暂不知原因

What’s the problem? The problem lies in the view.setTranslationX() function. The position of the view set by this function is not only visual, but also the actual position. So look at the code I implemented. When the view leaves the current interface, When the value of position is [-Infinity,-1] and [1,+Infinity], I did not use setTranslationX() to set its position outside the current interface, but it was still at the same position as the newly appeared view. It's just that it's invisible because the transparency is set with setAlpha().
How did I discover this problem? Just comment out all the code that calls selAlpha() in this class, run it again, and finally find that when I switch Fragments, two Fragments will appear overlapping and displayed. Now I modify this class as follows and the problem is solved (the temporary code is commented out):

import android.support.v4.view.ViewPager;
import android.view.View;

/*
*设置Fragment切换时的动画为淡入淡出
*/

public class NoSlidingPageTransformer implements ViewPager.PageTransformer {

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();    //得到view宽

        if (position <= -1) { // [-Infinity,-1]
            // This page is way off-screen to the left. 出了左边屏幕
            view.setTranslationX(0);

        } else if (position < 1) { // (-1,1)
            view.setTranslationX(-pageWidth * position);  //阻止页面的滑动,位置在左则设向右偏移位置,在右则设向左偏移位置
            float alphaFactor = 1 - Math.abs(position);
            //透明度改变
            view.setAlpha(alphaFactor);
            /*
            if (alphaFactor == 0)
                view.setVisibility(View.INVISIBLE);     //页面不在当前界面显示,则使其Invisible,这句是为了解决Fragment对上下滑动事件监听的错乱,暂不知原因
            else if (view.getVisibility() == View.INVISIBLE)
                view.setVisibility(View.VISIBLE);       //页面在当前界面显示,则使其Visible,这句是为了解决Fragment对上下滑动事件监听的错乱,暂不知原因
                */
        } else { // [1,+Infinity]
            // This page is way off-screen to the right.    出了右边屏幕
            view.setTranslationX(0);
        }
    }

}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template