Android image scrolling and tab control function implementation code

First of all, as an aside, when I got up this morning, I slipped my hand and threw my mobile phone out. As a result, the Motorola Milestone generation who accompanied me for two and a half years rested. So I decided to remember my dead love of the machine today.

If you are an online shopper, you must have a Taobao client on your mobile phone. People who pay attention to special effects will find that Taobao, whether it is a website or a mobile client, will have a picture scrolling player on its home page, which displays some of its recommended products. This function, which can almost be named by Taobao, looks very dazzling. Let's realize it today.

The implementation principle is actually the sideslip menu effect of Android imitating everyone's client sliding menu in the previous article. The simplest sideslip implementation in history is another variant based on that principle. It is the so-called all-round way. After really mastering a method, you can use this method to transform various impassable effects.

Today, we still implement a custom control, and then we can reference it in the layout file of any activity to achieve the effect of picture scroller.

Create a new Android project in eclipse called slidingviewswitcher.

Create a new class called slidingswitcherview, which inherits from relativelayout and implements the ontouchlistener interface. The specific code is as follows:

Careful friends can see that I still reuse a lot of previous code. Here are a few important points. In the onlayout method, redefine the size of each control containing pictures, and then register a touch event listener for each control containing pictures. In this way, when we slide any picture control, the ontouch event will be triggered, and then the animation effect will be realized by changing the leftmargin of the first picture control. After that, the tab view is dynamically added in onlayout. Several picture controls will add several tabs, and then determine which tab to highlight according to currentitemindex. There's nothing else to explain. For a deeper understanding, let's look at the code and comments.

Then let's take a look at how to use this custom control in the layout file to create or open an activity_ Main.xml, add the following code:

We can see that two linearlayouts are placed in the root directory of com.example.viewswitcher.slidingswitcherview. In the first LinearLayout, we need to put the pictures that need to be scrolled. Here, we add four buttons, and each button is set with a background picture. The second linear layout does not need to add anything. As long as the size and position are controlled, the label will be automatically added to the layout when running.

Then create or open mainactivity as the main interface without adding any new code:

Finally, the code of androidmanifest.xml is given, which is also automatically generated:

Well, now let's look at the operation effect. Because the mobile phone is broken, it can only run on the simulator.

First, when the program is opened, the interface is shown as follows:

Then slide your finger on the picture, and we can see the effect of picture scrolling:

If you keep turning the page, the tab will also change. In the following figure, we can see that the highlighted points are transformed:

Well, compared with the effect of Taobao client, I think our imitation is still very good. Eh, there seems to be something missing...... The original picture will not play automatically.....

It doesn't matter. I added the function of automatic playback in a later article, and it's not just the function of automatic playback. Please refer to Android's function of using custom attributes to realize automatic playback and scrolling of pictures.

That's all for today's article. If you have questions, please leave a message below.

Please click here to download the source code

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>