Example code of tablayout custom selection background slider in Android

Tablayout is a control in the material design package of Android, which can be matched with the viewpager in the V4 package to produce a linkage effect. Here I have customized a sliderlayout in which the slider can follow the tablayout for sliding selection. The effect is shown in the following figure (white box):

The following is the source code of sliderlayout:

One of the key classes is slideronpagechangelistener, which inherits tablayout Tablayoutonpagechangelistener class. Let's look at the source code (below). This is an interface to listen to the sliding selection of viewpager. What we need to do is to extend this class so that sliderlayout can also listen to the sliding of viewpager.

The method to calculate the sliding distance required for each sliderlayout is calculatescrollxfortab (int position, float positionoffset) (see the source code for details). Carry out relevant calculations according to the monitored viewpager sliding and slide sliderlayout

When applying, you must pay attention to viewpager setOnPagechangelistener(new SliderLayout.SliderOnPagechangelistener(mTabLayout,layout)); To create a layout in mtablayout setupWithViewPager(viewPager); After that:

Layout:

The above is the example code of tablayout custom selection background slider in Android introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>