Solution of Android nested sliding conflict

Android will cause sliding conflict when nested sliding. I met it before, but my previous notebook was lost, so I had to write another chapter.

1、 Sliding conflict will occur

So when will sliding conflict occur? For example, if you have an activity, the upper part of the activity is a layout, and the lower part is a slidable control (recyclerview, listview, etc.), or the lower part is a viewpager, and the fragment layout inside is a slidable control, so the page will have sliding conflict.

2、 Previous practice

Although my previous notes were lost, I still remember the idea of solving the problem at that time.

(1) Rewrite a viewpager of the viewpager inheritance system. I don't remember how to rewrite it

(2) Rewrite the recyclerview of the system inherited by the recyclerview. I remember that the recyclerview will not display or only display one item if the recyclerview does not set a fixed height. Therefore, rewrite the recyclerview to dynamically measure the height of item x count.

Although it could be solved at that time, the final effect was very twisted.

3、 Current practice

Now I certainly won't do the same as before, because a new control nestedscrollview has been created. It can help us solve sliding conflicts. Next, I will try my best to analyze all possible situations.

1. Only recyclerview is nested in the layout

As shown in the figure below:

The easiest way to solve this problem is to directly use nestedscrollview as the parent layout, and then nest recyclerview.

That's it. Remember two things:

(1) Add Android: fillview = "true" to the nestedscrollview of the parent layout, and then the recyclerview will not be displayed. The reason why it will not be displayed is that the recyclerview is a dynamically displayed view, and if it is used directly, it will be flattened as I said before, so add this attribute to make the child view display match_ Parent effect.

(2) Some people say that setnestedscrolling enabled (false) should be set for recyclerview, otherwise it will get stuck when sliding. I haven't tried this. I set true. At present, I don't think it will have any impact when sliding.

2. Layout nesting of other scrollable controls

In the first case, the recyclerview is replaced with other slidable controls.

To put it bluntly, you need nestedscrollview to be useful, because the key to resolving sliding conflicts lies in the two interfaces nestedscrollingparent and nestedscrollingchild (which will be described in detail below)

Both recyclerview and nestedscrollview implement nestedscrollingchild interface and internally encapsulate the logic processing to solve sliding conflict. Therefore, only nestedscrollview directly nested recyclerview or nestedscrollview will not produce sliding conflict.

The usage of nestedscrollview is the same as that of recyclerview. Remember to add those attributes.

3. Layout nested viewpager, viewpager nested recyclerview and other slidable controls

This situation is more troublesome to deal with, and many people encounter this situation. As shown below:

In fact, I wrote an article before that can solve this situation, that is, using coordinatorlayout. Using coordinatorlayout can solve this situation. However, as I said in my article, coordinatorlayout has a bug, which is stuck like shit. Whether you can bear it or not, I can't bear it anyway, so I won't use coordinatorlayout.

There are three solutions without coordinatorlayout:

(1) Use the open source custom coordinatorlayout on GitHub to solve the problem. I forgot the name.

But our boss said it's best not to use other people's open source views. So I can only use the second method.

(2) Discard viewpager

Why, because the viewpager of the system can't do it, it is said that the conflict can be resolved because nestedscrollingparent and nestedscrollingchild, and if nestedscrollingchild's ViewGroup implements the view of nestedscrollingparent interface, nestedscrollingparent's childview implements the view of nestedscrollingchild interface.

The parent layout in the figure is separated from the recyclerview by a viewpager, that is, the childview of nestedscrollingparent is viewpager, and the ViewGroup of nestedscrollingchild is viewpager. Therefore, the case of directly nesting a layer of viewpager cannot solve the sliding conflict.

There is a very direct way to solve the sliding conflict by using the fragment manager instead of the viewpager.

The FrameLayout here is to display the FrameLayout to the framemanager.

This can solve the sliding conflict when one activity has multiple fragments.

But some friends say no, I want viewpager, and I want cool sliding animation effects. Alas, then use the most practical third method.

(3) Custom

There's no way. Use customization. Customize a ViewGroup to implement nestedscrollingparent interface, and then customize a view to implement nestedscrollingchild interface. Or you can use nestedscrollview in the outer layer and customize viewpager in the inner layer to implement nestedscrollingchild interface.

You think it's over? Of course it's not that simple. These methods are available in the nestedscrollingchild interface.

You need to write the logic of handling sliding conflict in these methods. You can refer to recyclerview or find it on the Internet. Some great gods on the Internet really have introductions, but some people either copy others blindly, or just use coordinatorlayout. In fact, I'm not very good at handling the details inside. I just know the process, so I don't install x anymore.

4、 Other problems in use

Not to solve the sliding conflict, there are no other problems.

1. Nestedscrollview (recyclerview) will automatically scroll to the bottom when reloading data.

If you encounter this situation, just set. Scrollto (0,0) to the nestedscrollview of the parent layout, which is the same as Scrollview.

2. No sliding.

If you want to disable nestedscrollview sliding in some cases, you can add listening in nestedscrollview of the parent layout like processing Scrollview. For example, I:

This method is to set slidable and non slidable.

3. Remember to set Android: fillviewport = "true"

If your nested layout is not displayed, you may forget to set the Android: fillviewport property for the parent layout nestedscrollview.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>