In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to solve nested sliding conflicts in Android". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve nested sliding conflicts in Android".
one。 There will be a sliding conflict.
So when will there be a sliding conflict? For example, if you have an activity,activity where the top half is a layout, the bottom half is a slidable control (RecyclerView, ListView, etc.), or the lower half is a viewpager, and the fragment layout is a slidable control, there will be sliding conflicts on the page.
two。 The previous practice
Although my previous notes were lost, I still remember the way to solve the problem at that time.
(1) rewrite the ViewPager of a viewpager inheritance system. I don't remember how to rewrite it.
(2) rewrite the RecyclerView of the RecyclerView inheritance system, because I remember that there will be a height reason that if the RecyclerView does not set a fixed height, it will not display or only show an Item, so rewrite RecyclerView to dynamically measure the height of the Item x count.
Although it could be solved at that time, the final effect was very twisted.
three。 What we are doing now
Now I'm definitely not going to do it like I did before, because there's a new control, NestedScrollView. It can help us solve the sliding conflict very well, and then I will try my best to analyze all possible situations.
1. Case where only RecyclerView is nested in the layout
This is the case in the following picture:
This situation is easiest to solve, just use NestedScrollView as the parent layout, and then nest RecyclerView.
That's fine. Remember to keep two things in mind:
(1) add android:fillViewport= "true" to the parent layout NestedScrollView, and then the RecyclerView will not be displayed, because RecyclerView is a dynamic display View, and if you use it directly, it will be flattened, so add this attribute to let the child View show the effect of match_parent.
(2) some people say that they want to set setNestedScrollingEnabled (false) for RecyclerView, otherwise it will get stuck when sliding. I haven't tried this. I set true. At present, it doesn't have any effect when sliding.
two。 Layout nesting of other scrollable controls
Is to replace the RecyclerView with other slidable controls in the first case.
To put it bluntly, you need to use NestedScrollView to be useful, because the key to resolving sliding conflicts lies in the NestedScrollingParent and NestedScrollingChild interfaces (more on this below).
On the other hand, both RecyclerView and NestedScrollView implement the NestedScrollingChild interface and encapsulate the logic processing to solve sliding conflicts internally, so only NestedScrollView nesting RecyclerView or NestedScrollView directly will not cause sliding conflicts.
NestedScrollView is used in the same way as RecyclerView. Remember to add those attributes.
3. Layout nesting ViewPager,ViewPager nesting RecyclerView and other slidable controls
This kind of situation is troublesome to deal with, and many people encounter this situation. As shown below:
That is, using CoordinatorLayout, using CoordinatorLayout can solve this situation.
But CoordinatorLayout has BUG, which is stuck like shit, whether you can stand it or not, I can't stand it, so I won't use CoordinatorLayout.
There are three solutions without CoordinatorLayout:
(1) use the custom CoordinatorLayout open source on github to solve the problem. I forgot what it was called.
But our boss said, it's best not to use someone else's open source View. So I had to use the second method.
(2) give up using ViewPager
Why, because the ViewPager of the system can not do so, it is mentioned above that the conflict can be resolved because of NestedScrollingParent and NestedScrollingChild, and if the ViewGroup of NestedScrollingChild implements the ChildView of View,NestedScrollingParent of NestedScrollingParent interface, if it implements the View of NestedScrollingChild interface.
The parent layout in the figure is separated from the RecyclerView by a ViewPager, which means that the ChildView of NestedScrollingParent is ViewPager,NestedScrollingChild and the ViewGroup is ViewPager. So the situation of directly nesting a layer of ViewPager cannot solve the sliding conflict.
There is a very direct way is not to use ViewPager, use FragmentManager, so that you can solve the sliding conflict.
..
The FrameLayout here is for FragmentManager to display FrameLayout.
This resolves sliding conflicts in the case of multiple fragment in a single activity.
But some friends say no, I want Viewpager, I want cool sliding animation effects. Well, then you should use the third method which is the most practical.
(3) Custom
There is no way, then use custom, customize a VIewGroup implementation NestedScrollingParent interface, and then customize a View implementation NestedScrollingChild interface. Or you can use NestedScrollView on the outside and customize ViewPager on the inside to implement the NestedScrollingChild interface.
You think that's it? Of course it's not that simple. There are these methods in the NestedScrollingChild interface.
You need to write your own logic to deal with sliding conflicts in these methods. You can refer to RecyclerView to write, or you can find it on the Internet. There are some great gods on the Internet who really have introductions, but there are also some people who either copy others' but not finish them, or just say they use CoordinatorLayout. In fact, I am not very good at dealing with the details. I just know the process, so I don't install X.
four。 Other problems in use
It is not that there are no other problems if you solve the sliding conflict.
1.NestedScrollView (RecyclerView) automatically scrolls to the bottom when the data is reloaded.
If you encounter this situation, just set .scrollto (0,0) for the NestedScrollView of the parent layout, the same as ScrollView.
two。 Do not slide.
If you want to disable NestedScrollView sliding under certain circumstances, you can add listening in the NestedScrollView of the parent layout, such as mine, just as you would with ScrollView:
Public void isScroll (boolean bol) {Nsv.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {return! bol;}});}
This method is to set slideable and non-slipperable.
3. Remember to set android:fillViewport= "true"
If your nested layout is not displayed, you may have forgotten to set the android:fillViewport property for the parent layout NestedScrollView.
Thank you for your reading, the above is the content of "how to solve nested sliding conflicts in Android". After the study of this article, I believe you have a deeper understanding of how to solve the problem of nested sliding conflicts in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.