[Android] Using ViewPager for navigation, androidviewpager

Source: Internet
Author: User

[Android] Using ViewPager for navigation, androidviewpager

 

Reprint Please Note: Reproduced in http://www.cnblogs.com/Liuyt-61/p/6582667.html

------------------------------------------------------------------

 

First, you can see that ViewPager achieves a sliding effect on the left and right sides of the view.

The declaration of ViewPager in XML files is a little different from that of other controls.
Android. support. v4.view is a jar package loaded by a third party with special effects on the Android interface, which is backward compatible.
<android.support.v4.view.ViewPager        ...        ...></android.support.v4.view.ViewPager>

Load the displayed page card: You need to transform the layout file into a View object

(1)LayoutInflater lf = getLayoutInfalter().from(this);lf.inflate(resource,root);
(2)View.inflate(context,resource,root);

PagerTabStrip and PagerTitleStrip

You can also see the name. The two names are used to set the Title, just like the directory (, Address Book, discovery, and me) displayed at the bottom of the main interface. They can navigate to the page Title that the user slides.

<Android. support. v4.view. viewPager...> <android. support. v4.view. pagerTabStrip...> </android. support. v4.view. pagerTabStrip> </android. support. v4.view. viewPager> <! -- PagerTitleStrip is defined in the same way as PagerTabTitle. In ViewPager, -->

Generally, you can use both of them. If both are used, PagerTabStrip becomes invalid.

Of course, PagerTabStrip and PagerTitleStrip also beautify the navigation directory by setting some attributes.

                private PagerTabStrip tabStrip;                      tabStrip = (PagerTabStrip) findViewById(R.id.tabStrip);                tabStrip.setBackgroundColor(Color.GRAY);             tabStrip.setTextColor(Color.BLACK);                tabStrip.setDrawFullUnderline(false);                tabStrip.setTabIndicatorColor(Color.GREEN);                    

 

There are three different adapters for ViewPager, and the corresponding data sources are also different.

(1) PagerAdapter ---- Data source: List <View>

 

Package com. liuyt. s03_e19_viewpager; 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> viewList; private List <String> titleList; public MyPagerAdapter (List <View> viewList, List <String> titleList) {this. viewList = viewList; this. titleList = titleList;}/** get the number of page cards */@ Override public int getCount () {// TODO Auto-generated method stub return viewList. size ();}/** determine whether the View belongs to the Object */@ Override public boolean isViewFromObject (View arg0, Object arg1) {// TODO Auto-generated method stub return arg0 = arg1;}/** instantiate a page card */@ Override public Object instantiateItem (ViewGroup container, int position) {// TODO Auto-generated method stub container. addView (viewList. get (position); return viewList. get (position);}/** destroy a page card */@ Override public void destroyItem (ViewGroup container, int position, Object object) {// TODO Auto-generated method stub container. removeView (viewList. get (position);}/** set the title of viewPager */@ Override public CharSequence getPageTitle (int position) {// TODO Auto-generated method stub return titleList. get (position );}}

 

(2) FragmentPagerAdapter ---- Data source: List <Fragment>

package com.Liuyt.s03_e19_viewpager;import java.util.List;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;public class MyFragmentPagerAdapter extends FragmentPagerAdapter{    private List<Fragment>fragList;    private List<String>titleList;        public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment>fragList,List<String>titleList) {        super(fm);        // TODO Auto-generated constructor stub        this.fragList = fragList;        this.titleList = titleList;    }    @Override    public Fragment getItem(int arg0) {        // TODO Auto-generated method stub        return fragList.get(arg0);    }    @Override    public int getCount() {        // TODO Auto-generated method stub        return fragList.size();    }    @Override    public CharSequence getPageTitle(int position) {        // TODO Auto-generated method stub        return titleList.get(position);    }}

 

(3) FragmentStatePagerAdapter ---- Data source: List <Fragment>

No code will be posted here. The Code of FragmentStatePagerAdapter is not much different from that of FragmentPagerAdapter. The difference between the code and the code is that a method that will call destroy to destroy the page card after sliding, the other does not destroy the page card.

 

Finally, the listener. ViewPager uses the OnPagerChangeListener listener to listen to the page to which the listener slides. The common method in the middle is public void onPagerSelected (int arg0 ){}

 

Related Article

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.