"Android Interface Implementation" uses Pagertabstrip to implement sliding label Viewpager

Source: Internet
Author: User

There are always a lot of articles that can be done on a sliding control such as Viewpager. The last article. We have implemented a viewpager indicator of our own definition. In this article, we mainly want to take advantage of Android's own control, implement an indicator, this control, is the SUPPORT-V4 package inside the Pagertabstrip control.

First, let's take a look at the implementation effect.




Can see, the effect of the implementation is also very good. More fluid than previously defined label indicators. Here's a brief introduction to Pagertabstrip and its use.

Pagertabstrip is the class within the V4 support package, which is a Viewpager class and cannot be used anywhere else. At the time of use. We just need to declare it in the Viewpager layout.

As in the following code

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:background = "@android: Color/white"    android:orientation= "vertical" >    <android.support.v4.view.viewpager        Android:id= "@+id/viewpager"        android:layout_width= "match_parent"        android:layout_height= "Match_parent" >        <android.support.v4.view.pagertabstrip            android:layout_width= "match_parent"            android:layout _height= "Wrap_content"            android:layout_gravity= "top"            android:background= "@android: Color/holo_blue_ Light "            android:textcolor=" @android: Color/white "/>    </android.support.v4.view.viewpager></ Linearlayout>

We can set the Layout_gravity property, for example, top or bottom, to control where the display is. In addition, no other settings are required.

The rest. is what we need to implement in the code.

Import Java.util.arraylist;import java.util.list;import android.os.bundle;import android.support.v4.app.Fragment; Import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentpageradapter;import Android.support.v4.view.viewpager;public class Pagertabstripactivity extends Fragmentactivity {private List< fragment> fragments;private viewpager viewpager;private list<string> titlelist; @Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Initview ();} private void Initview () {Viewpager = (Viewpager) Findviewbyid (r.id.viewpager); titlelist = new arraylist<string> () ; Titlelist.add ("newest recommendation"); Titlelist.add ("Game Entertainment"); Titlelist.add ("AV video"); fragments = new arraylist<fragment> (); Fragments.add (New Myfragment (Android). R.color.holo_green_light)); Fragments.add (new Myfragment (Android). R.color.holo_orange_light)); Fragments.add (new Myfragment (Android). R.color.holo_red_light)); Fragmentpageradapter AdapteR = new Fragmentpageradapter (Getsupportfragmentmanager ()) {@Overridepublic int getcount () {return 3;} @Overridepublic Fragment getItem (int position) {return fragments.get (position);} @Overridepublic charsequence getpagetitle (int position) {return titlelist.get (position);}}; Viewpager.setadapter (adapter);}}

In the code above. Showing how we control the text of the headline, we just need to rewrite the Getpagertitle method. Suppose you don't want the following indicators, change the Pagertabstrip to Pagertitlestrip.

Able to put the rest. Just give it to the great Android system.

"Android Interface Implementation" uses Pagertabstrip to implement sliding label Viewpager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.