Android about Viewpager combined fragment fragment simple use

Source: Internet
Author: User



I. First add the Viewpager control to XML


We want each Viewpager to display a picture

Create a new Pager_item.xml file


The code is as follows





Two. Analysis

A viewpager needs to set up an adapter that can inherit Fragmentstatepageradapter,

The adapter also needs to set the data, which adds fragmentation to the adapter. Fragments can inherit fragment, set views, listen, and so on, as shown below:




Three. We need to create a new fragment first.

Note that this inherited Fragment need to be under package android.support.v4.app.Fragment

Fragments and activity are similar, have their own life cycle, needless to say, the knowledge of debris can find information on their own.


The code is as follows: Key methods three

1: This is the setting of the view


2.: This is the setting of the picture, according to the number of the different settings of different pictures (number by mainactivity Pass in)


3 This is to accept the number in the outside, and set the number in the bundle



Four. Write an adapter

The fragment is written, now you need to write an adapter


There are two main ways to write an adapter:

Five. Set the shards in Mainactivity



So it's done.

SOURCE Download:

http://pan.baidu.com/share/link?shareid=3331882871&uk=705537436



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.