My android learning experience 26 and my android learning experience 26

Source: Internet
Author: User

My android learning experience 26 and my android learning experience 26

Use of ViewPager

ViewPager is the same as the navigation bar on the top of qq. Sliding can be changed to different views or Fragment.

Usage:

Define tags in the layout file:

Android. support. v4.view. ViewPager is the ViewPager control, and PagerTabStrip is the title bar of the control, as shown in the yellow section

<Android. support. v4.view. ViewPager
Android: id = "@ + id/viewPager"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center">

<Android. support. v4.view. PagerTabStrip
Android: id = "@ + id/tab"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "top"
>
</Android. support. v4.view. PagerTabStrip>
</Android. support. v4.view. ViewPager>

 

Create a class that inherits PagerAdapter and implement PagerAdapter in this class

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> dataList; // View source to be loaded, defined in activityzhong
Private List <String> listStrings; // Title Bar source, defined in activity

Public MyPagerAdapter (List <View> dataList, List <String> listStrings ){
This. dataList = dataList;
This. listStrings = listStrings;
}


@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return dataList. size ();
}

@ Override
Public boolean isViewFromObject (View arg0, Object arg1 ){
// TODO Auto-generated method stub
Return arg0 = arg1;
}

@ Override
Public Object instantiateItem (ViewGroup container, int position ){
// TODO Auto-generated method stub
Container. addView (dataList. get (position ));
Return dataList. get (position );
}
@ Override
Public void destroyItem (ViewGroup container, int position, Object object Object ){
// TODO Auto-generated method stub
Container. removeView (dataList. get (position ));
}

@ Override
Public CharSequence getPageTitle (int position ){
// TODO Auto-generated method stub
Return listStrings. get (position );
}
}

 

Main activity:

Import java. util. ArrayList;
Import java. util. List;
Import java.util.zip. Inflater;

Import android. R. color;
Import android. app. Activity;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. support. v4.view. PagerAdapter;
Import android. support. v4.view. PagerTabStrip;
Import android. support. v4.view. ViewPager;
Import android. view. View;

Public class MainActivity extends Activity {

Private ViewPager viewPager;
Private PagerTabStrip tabStrip; // set the title bar of ViewPager.
Private List <String> listStrings;


Private List <View> dataList;

Private View view1; // This View is defined in the layout file.
Private View view2;
Private View view3;

Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
ViewPager = (ViewPager) findViewById (R. id. viewPager );
TabStrip = (PagerTabStrip) findViewById (R. id. tab );

View1 = View. inflate (this, R. layout. view1, null );
View2 = View. inflate (this, R. layout. view2, null );
View3 = View. inflate (this, R. layout. view3, null );

DataList = new ArrayList <View> ();
DataList. add (view1 );
DataList. add (view2 );
DataList. add (view3 );

ListStrings = new ArrayList <String> ();
ListStrings. add ("first ");
ListStrings. add ("second ");
ListStrings. add ("third ");

TabStrip. setBackgroundColor (Color. YELLOW );
TabStrip. setTextColor (Color. BLACK );
TabStrip. setDrawFullUnderline (false );
TabStrip. setTabIndicatorColor (Color. GREEN );
TabStrip. setVerticalFadingEdgeEnabled (false );

MyPagerAdapter adapter = new MyPagerAdapter (dataList, listStrings );
ViewPager. setAdapter (adapter );
}
}

It may be a bit confusing. If you don't understand it, you can leave a message for me. Thank you.

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.