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>
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:This piece of code is what I temporarily used to solve the "supernatural" phenomenon described in the question:
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):