Android Multi-screen slideshow: Viewpager based on the use of birth defects with Pagertabstrip (source code)

Source: Internet
Author: User
Tags object object

Recent use of Viewpager. Read a few people say is not very clear information, simply write your own demo summary under.

Examples are very easy. There are three interfaces in the activity that slide. Each interface has a button and is set up for listening. The Pagertabstrip is the identification line that is sliding. Below the view. Below is the code:

Activity_main.xml (main layout)

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ".        Mainactivity "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:text= "@string/hello_world"/> <android.support.v4.view.viewpager android:id= "@+id/viewpager "Android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:layout_centerinpar Ent= "true" > <android.support.v4.view.pagertabstrip android:id= "@+id/pagertab" Android: Layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_gravity= "Bottom"/> </android.support.v4.view.ViewPager>< /relativelayout>

Here are three view layouts that are almost identical:

First_pager_layout.xml

<?

XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:orientation=" Vertical " android:background=" @android: Color/holo_blue_bright "> <button android:id=" @+id/btn _in_first " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:text=" Please click " android:layout_gravity=" center "/></linearlayout>


Second_pager_layout.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical"     android:background= "@android: Color/holo_green_light" > <button     android:id= "@ +id/btn_in_second "    android:layout_width=" wrap_content "    android:layout_height=" Wrap_content    " android:text= "Please click"    android:layout_gravity= "center"/>   </LinearLayout>

Third_pager_layout.xml

<?xml version= "1.0" encoding= "Utf-8"?

><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match _parent " android:layout_height=" match_parent " android:orientation=" vertical " android:background=" @ Android:color/holo_red_light "> <button android:id=" @+id/btn_in_third " android:layout_width=" Wrap_content " android:layout_height=" wrap_content " android:text=" please click " android:layout_gravity=" Center "/></linearlayout>


Mainactivity.java (This is the main program.) Instantiate the Viewpager inside and set its adapter)

Package Org.yanzi.testviewpager1;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.content.context;import Android.graphics.color;import Android.os.bundle;import Android.support.v4.view.pagertabstrip;import Android.support.v4.view.pagertitlestrip;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.layoutinflater;import Android.view.menu;import Android.view.view;import Android.widget.Button;import Android.widget.toast;public class Mainactivity extends Activity {list<view> mviews = new arraylist<view> (); String[] mtitles = {"Page 1", "Page 2", "Page 3"}; Mypageradapter Pageradapter; Pagertabstrip Pagertabstrip; Pagertitlestrip Pagertitlestrip; Viewpager Viewpager; View View1, View2, VIEW3; Button btn1, btn2, btn3; Btnlistener btnlistener; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Initui (); LayOutinflater Flater = (layoutinflater) this.getsystemservice (context.layout_inflater_service); view1 = Flater.inflate ( R.layout.first_pager_layout, null); view2 = Flater.inflate (r.layout.second_pager_layout, null); VIEW3 = Flater.inflate (r.layout.third_pager_layout, null); Mviews.add (View1); Mviews.add (VIEW2); Mviews.add (view3);p ageradapter = new Mypageradapter (Mviews, mtitles); Viewpager.setadapter (Pageradapter); Viewpager.setcurrentitem (1);// Set Pagertabstrippagertabstrip.settabindicatorcolor (color.red);p Agertabstrip.settextcolor (Color.RED); Pagertabstrip.setclickable (False);p agertabstrip.settextspacing (;p) Agertabstrip.setbackgroundcolor (Color.GRAY );p Agertabstrip.setdrawfullunderline (true);/*pagertitlestrip.settextcolor (color.red); Pagertitlestrip.settextspacing (*///viewpager), Slide monitor viewpager.setonpagechangelistener (New OnPageChangeListener ( {@Overridepublic void onpageselected (int arg0) {//TODO auto-generated method Stubshowtoast ("Switch to:" + mtitles[arg0]);} @Overridepublic void onpagescrolled (inT arg0, float arg1, int arg2) {//TODO auto-generated method stub} @Overridepublic void onpagescrollstatechanged (int arg0) {//TODO auto-generated Method stub}}); Initbtns ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} private void Initui () {Viewpager = (Viewpager) Findviewbyid (r.id.viewpager);p Agertabstrip = (pagertabstrip) Findviewbyid (r.id.pagertab);//pagertitlestrip = (Pagertitlestrip) Findviewbyid (r.id.pagertab);} private void Initbtns () {if (view1! = null) {BTN1 = (Button) View1.findviewbyid (R.id.btn_in_first);} if (view2! = null) {BTN2 = (Button) View2.findviewbyid (R.id.btn_in_second);} if (VIEW3! = null) {Btn3 = (Button) View3.findviewbyid (R.id.btn_in_third);} Btnlistener = new Btnlistener (); Btn1.setonclicklistener (Btnlistener); Btn2.setonclicklistener (BtnListener); Btn3.setonclicklistener (Btnlistener);} Class Btnlistener implements view.onclicklistener{@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {Case R.id.btn_in_first:showtoast (" You clicked the first interface "), Break;case r.id.btn_in_second:showtoast (" You clicked the 2nd interface "), Break;case r.id.btn_in_third:showtoast (" You clicked the 3rd interface "); break;default:break;}}} private void Showtoast (String s) {Toast.maketext (Getapplicationcontext (), S, Toast.length_short). Show ();}}

Mypageradapter.java (This is the Viewpager adapter that inherits the Pageradapter and writes it to a file separately)

Package Org.yanzi.testviewpager1;import Java.util.list;import Android.support.v4.view.pageradapter;import Android.view.view;import Android.view.viewgroup;public class Mypageradapter extends Pageradapter {private List< View> mlistviews;private string[] mtitles;public mypageradapter (list<view> views, String[] titles) { This.mlistviews = Views;this.mtitles = titles;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn mlistviews.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {//TODO auto-generated method Stubreturn (ARG0==ARG1);} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {//TODO auto-generated method Stubcont Ainer.removeview (Mlistviews.get (position));} @Overridepublic Object Instantiateitem (viewgroup container, int position) {//TODO auto-generated method Stubcontainer.addview (Mlistviews.get (position), 0); return mlistviews.get (position);} @Overridepublic charsequence getpagetitle (int position) {TODO auto-generated method Stubreturn mtitles[position];}} 

key points:

1, Viewpager plainly is a control, in the use of the package name to be full of android.support.v4.view.ViewPager.

Because my Adt-bundle version number is relatively high. This package comes with the default, and is exported by default with APK packaging.

For example, with:


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvewfuemkxmji1nji3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>

If you do not have this package in the Android Private libraries, you should add it yourself in the libraries of the attribute.

When you join, remember to tick it in the order and export you see.

2. Theoretically, to realize the sliding screen only need a viewpager to be able to, do not need to nest inside such as the following:

<android.support.v4.view.pagertabstrip
Android:id= "@+id/pagertab"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_gravity= "Bottom"/>

This pagertabstrip is a horizontal line, assuming that you want to use it to identify the current view in which to add it, of course, eventually you will find that this is a pit-father thing.

Except Pagertabstrip. Another pagertitlestrip, the use of both the same position is the same, should be nested in the Viewpager. The difference is:

A, Pagertabstrip included in the effect of pagertitlestrip, assuming that only join Pagertabstrip can see only the cable, but it occupies a certain height of the layout.

The default is not to display the title, assuming that the display needs to be rewritten in the adapter:

@Override
Public charsequence getpagetitle (int position) {
TODO auto-generated Method Stub
return mtitles[position];
}

The title will be displayed. The color of the title and the line, and the entire identity view background can be set in the code again. Demo sample is shown in the demo.

B, Pagertitlestrip only show the title and not the line.

C, Pagertabstrip can click Switch View, and Pagertitlestrip cannot click. Many other links to see

The two are relative to the position of the Father Viewpager, that is, the logo is above or below the view. Set by Pagertabstrip's properties android:layout_gravity= "bottom".

3, just like the ListView, the key to Viewpager is the adapter, and to use normal usage requires at least the following four methods of rewriting:

@Overridepublic int GetCount () {//TODO auto-generated method Stubreturn mlistviews.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {//TODO auto-generated method Stubreturn (ARG0==ARG1);} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {//TODO auto-generated method Stubcont Ainer.removeview (Mlistviews.get (position));} @Overridepublic Object Instantiateitem (viewgroup container, int position) {//TODO auto-generated method Stubcontainer.addview (Mlistviews.get (position), 0); return mlistviews.get (position);}

The following method is used to display the title, generally do not rewrite, because Pagertabstrip is a useless thing.

@Overridepublic charsequence getpagetitle (int position) {//TODO auto-generated method Stubreturn mtitles[position];}

4. After setting the adapter. by Viewpager.setcurrentitem (1); To set the default Viewpager display which view.

1 identifies a second interface.

5. Use child controls in each view. by Initbtns () to get, note Findviewbyid when must be preceded by the father. For example, the following:

BTN1 = (Button) View1.findviewbyid (R.id.btn_in_first); the more rigorous approach is that the control in this view is capable of being monitored only when the current view is displayed.


6, when the Viewpager view changes, set the monitoring:

Viewpager Slide Monitor
Viewpager.setonpagechangelistener (New Onpagechangelistener () {


@Override
public void onpageselected (int arg0) {
TODO auto-generated Method Stub
Showtoast ("Switch to:" + mtitles[arg0]);
}


@Override
public void onpagescrolled (int arg0, float arg1, int arg2) {
TODO auto-generated Method Stub


}


@Override
public void onpagescrollstatechanged (int arg0) {
TODO auto-generated Method Stub


}
});

7, Pagertabstrip in the very many methods are not effective such as pagertabstrip.settextspacing (40);

Pagertabstrip.setdrawfullunderline (TRUE);

The following are:




Can see the Display Page 3 o'clock, the following pagetabstrip actually happened to shift, no one will need this experience, is really a pit father of the thing AH. Originally I still do not want to find out not to write their own pagetabstrip situation to solve the problem. See the link completely abandoned. About the logo or to write it yourself, perhaps the article will give a full demonstration sample.

Code Download: http://download.csdn.net/detail/yanzi1225627/7223287




Android Multi-screen slideshow: Viewpager based on the use of birth defects with Pagertabstrip (source code)

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.