Viewpager+fragment implementation of Android in the screen left and right switch (Advanced article)

Source: Internet
Author: User

Fragment supports the use of different activity and can handle its own input events and life cycle methods. Can be seen as a child activity.

First look at the layout:

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Tools:context=". Viewpageractivity " >6 7     <Android.support.v4.view.ViewPager8         Android:id= "@+id/viewpager"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One android:layout_gravity= "Center" > A     </Android.support.v4.view.ViewPager> -  - </LinearLayout>

Main file: viewpageractivity inheritance Fragmentactivity

1  PackageCom.example.viewpagerdemo;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 ImportAndroid.os.Bundle;7 Importandroid.support.v4.app.Fragment;8 Importandroid.support.v4.app.FragmentActivity;9 ImportAndroid.support.v4.view.ViewPager;Ten  One ImportCom.example.adapter.fragmentAdapter; A  -  Public classViewpageractivityextendsfragmentactivity { -  the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (R.layout.activity_view_pager); +list<fragment> fragments =NewArraylist<fragment>(); -          for(inti = 0; I < 3; i++) { +Fragment f =Newbuttonfragment (); A Fragments.add (f); at         } -Viewpager VP =(Viewpager) Findviewbyid (R.id.viewpager); -Fragmentadapter FA =NewFragmentadapter (Getsupportfragmentmanager (), - fragments); - Vp.setadapter (FA); -Vp.setcurrentitem (0); in     } -}

New Fragmentadapter inheritance Fragmentpageradapter does not write the implementation method

1  PackageCom.example.adapter;2 3 Importjava.util.List;4 5 Importandroid.support.v4.app.Fragment;6 ImportAndroid.support.v4.app.FragmentManager;7 ImportAndroid.support.v4.app.FragmentPagerAdapter;8 9  Public classFragmentadapterextendsFragmentpageradapter {Ten     PrivateList<fragment>fragments; One     PrivateFragmentmanager FM; A  -      PublicFragmentadapter (Fragmentmanager FM, list<fragment>fragments) { -         Super(FM); the          This. Fragments =fragments; -  -     } -  +      PublicFragmentadapter (fragmentmanager FM) { -         Super(FM); +          This. FM =FM; A     } at  - @Override -      PublicFragment GetItem (intarg0) { -         returnFragments.get (arg0); -     } -  in @Override -      Public intGetCount () { to         returnfragments.size (); +     } -  the}

Define the fragment content to be displayed, and place a button in it

1  PackageCom.example.viewpagerdemo;2 3 ImportAndroid.os.Bundle;4 Importandroid.support.v4.app.Fragment;5 ImportAndroid.view.LayoutInflater;6 ImportAndroid.view.View;7 ImportAndroid.view.ViewGroup;8 ImportAndroid.widget.TextView;9 Ten  Public classButtonfragmentextendsFragment { One     PrivateView Rootview;
12 - @Override - PublicView Oncreateview (layoutinflater inflater, ViewGroup container, the Bundle savedinstancestate) { -Rootview = Inflater.inflate (R.layout.guide1, container,false); - returnRootview;18 }19}

The implementation of 3 contains a button can be switched left and right

Viewpager+fragment implementation of Android in the screen left and right switch (Advanced article)

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.