Use of the Viewpager "Android-v"

Source: Internet
Author: User

Viewpager is a control in the Android V4 package, often used as the home page of the scrolling ads, but also often combined with fragment to achieve page switching effect.

There are many similarities between the Viewpager and the ListView, which are adapter controls, where you need to set up the adapter to display the data. There are two commonly used adapter classes for Viewpager: Pageradapter and Fragmentpageradapter, respectively, to display common layout pages and fragment pages. Let's introduce each of them.

1, Viewpager load normal page:

When we need to load a normal page with Viewpager, we need to set an adapter for Viewpager, which must be a subclass of Pageradapter, so we need to create a subclass that inherits from Pageradapter, Implement or override the GetCount (), Isviewfromobject (), Destroyitem (), Instantiateitem () methods in this class.

    • GetCount () Method: Gets the number of pages in the Viewpager;
    • Isviewfromobject () Method: whether the data in the Viewpager is directly loaded from the provided data source, the general return to the view = = object can be;
    • Destroyitem () Method: Delete This page from the Viewpager, which is usually to delete three previous pages, which is intended to be a resource recovery. What we need to do is take out the current page that needs to be destroyed from the data source and call the Removeview () method to delete it.
    • Instantiateitem () Method: Loads the page method. Create a page in this method, fit the data, and return.

The specific code is as follows:

 Public classMyPageAdapterextendsPageradapter {Private int[] resources; PrivateList<view>Views ; PrivateLayoutinflater Inflater;  PublicMyPageAdapter (Context context,int[] resources) {         This. resources =resources;  This. views =NewArraylist<>();  This. Inflater =Layoutinflater.from (context); } @Override Public intGetCount () {returnresources.length; } @Override Public Booleanisviewfromobject (View view, Object object) {returnView = =object; } @Override Public voidDestroyitem (ViewGroup container,intposition, Object object)    {Container.removeview (Views.get (position)); } @Override PublicObject Instantiateitem (ViewGroup container,intposition) {View View= Inflater.inflate (R.layout.pageritem_page, container,false); ImageView IV=(ImageView) View.findviewbyid (r.id.image);        Iv.setimageresource (Resources[position]);        Container.addview (view);        Views.add (view); returnview; }}

2, Viewpager loading fragment:

When we need to load fragment with Viewpager, we need to set the adapter class for Viewpager to be a subclass of the Fragmentpageradapter class, and implement the GetCount () method and the GetItem () method in it.

    • GetCount () Method: Gets the number of fragment in the set Viewpager
    • GetItem () Method: Gets the Fragment object that is currently loaded according to the position parameter

The specific code is as follows:

 Public classMypageradapterextendsFragmentpageradapter {PrivateList<fragment>fragments;  PublicMypageradapter (fragmentmanager FM) {Super(FM); }     Public voidSetfragments (list<fragment>fragments) {         This. Fragments =fragments; } @Override PublicFragment GetItem (intposition) {        returnFragments.get (position); } @Override Public intGetCount () {returnfragments.size (); }}

The above is the simple use of Viewpager introduction, the following sticker code cloud on the source, for your reference.

Demo Address

Use of the Viewpager "Android-v"

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.