Use of Android Learning--viewpager (i)

Source: Internet
Author: User

This section describes using the Fragmentpageradapter adapter to load the Fragment object.

Data source

When loading a fragment object, the data source naturally comes from fragment, similar to view, and still uses list to hold the data source

Fragmentlist=new arraylist<> ();
Fragmentlist.add (New Fragment1 ());
Fragmentlist.add (New Fragment2 ());
Fragmentlist.add (New Fragment3 ());
Fragmentlist.add (New Fragment4 ());

Adapter

Similar to Pageradapter, Fragmentpageradapter can also be overridden by a new subclass to implement the processing of the data source. You can simply rewrite the GetCount method and the GetItem method compared to the fragmentpageradapter. Another adapter for fragment, which corresponds to Fragmentstatepageradapter, needs to reload the Instantiateitem and Destoryitem methods, but only the default overloads are required.

 Public classMyfragmentadapterextendsFragmentpageradapter {PrivateList<fragment>fragmentlist;  PublicMyfragmentadapter (Fragmentmanager fm,list<fragment>fragmentlist) {        Super(FM);  This. fragmentlist=fragmentlist; } @Override PublicFragment GetItem (intposition) {        returnFragmentlist.get (position); } @Override Public intGetCount () {returnfragmentlist.size (); }}

Viewpager associating with adapters

You only need to instantiate the adapter and the Viewpager control in the activity and associate it with the Setadapter method.

Pager= (Viewpager) Findviewbyid (R.id.pager);
Myfragmentadapter myadapter =new Myfragmentadapter (Getsupportfragmentmanager (), fragmentlist);
Pager.setadapter (Myadapter);

Use of Android Learning--viewpager (i)

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.