android--using Fragmentpageradapter to realize the sliding effect of fragment

Source: Internet
Author: User

Effect:

ViewPage from Android-support.v4

    1. What is ViewPage?
      ViewPage is similar to a ListView used to display multiple view collections. Support page swipe left and right.

    2. How do I use viewpage and need to be aware of points?
      ViewPage needs adapter:
      There are four important methods of Pageradapter:
      (1) void Destroyitem (ViewGroup container, int position, Object object): Destroyed
      (2) Object Instantiateitem (viewgroup container, int position): Add view
      (3) int getcount (): Get View always
      (4) Boolean isviewfromobject (view view, Object obj): view comparison

      Listener: Addonpagechangelistener: Page changed
      (1) void onpageselected (int position): View toggles:p osition new page position
      (2) void onpagescrolled (int arg0, float arg1, int arg2): When the page is scrolling [sliding offset information], callback
      (3) void onpagescrollstatechanged (int arg0): callback When the page state is changed

The ListView uses the same view, so it is reused.
While ViewPage is based on view, subclasses have many differences, so to log off.

Public Object Instantiateitem (viewgroup container, int position) {
TODO auto-generated Method Stub
View view= imagevisws.get (position% images.length);
Container.removeview (view); Duplicate joins can cause exceptions
Container.addview (view);
return view;

Cause exception: Java.lang.IllegalStateException The specified child already have a parent. You must call Removeview ()
Cause: The added view has a parent class, so you must clear the parent class before you add it to another collection.
}

Activity_activityad.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.chenshuai.test322.Activityad">    <!--Viewpager -    <Android.support.v4.view.ViewPagerAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/viewpage">        <!--Pagertitlestrip Plus title -        <Android.support.v4.view.PagerTitleStripAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content">        </Android.support.v4.view.PagerTitleStrip>    </Android.support.v4.view.ViewPager></LinearLayout>
Layout_ad1.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/coco"        /></LinearLayout>
Layout_ad2.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/df"       /></LinearLayout>
Layout_ad3.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/hh"        /></LinearLayout>
Activityad.java
 Packagecom.example.chenshuai.test322;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;Importjava.util.ArrayList;Importjava.util.List; Public classActivityadextendsappcompatactivity {PrivateViewpager Viewpager; PrivateList<view>content; PrivateLayoutinflater Inflater; PrivateList<string>title; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_activityad); Viewpager=(Viewpager) Findviewbyid (r.id.viewpage); //Preparing DataContent =NewArraylist<view>(); //Inflater = Layoutinflater.from (this);Inflater =Getlayoutinflater (); /*** Add Content*/Content.add (Inflater.inflate (R.layout.layout_ad1,NULL)); Content.add (Inflater.inflate (r.layout.layout_ad2,NULL)); Content.add (Inflater.inflate (R.layout.layout_ad3,NULL)); /*** Add titles for each content title*/title=NewArraylist<string>(); Title.add ("Sports News"); Title.add ("Entertainment News"); Title.add ("Military News"); Viewpager.setadapter (NewViewadapter ( This, Content,title)); }     Public classViewadapterextendsPageradapter {Privatecontext Context; PrivateList<view>content; PrivateList<string>title; //override construct method, add picture, add caption         PublicViewadapter (context context, list<view>content, List<String>title) {             This. Context =context;  This. Content =content;  This. title =title; }        //must 1-get the number of controls to slide, here we take the sliding banner as an example, then this should be the number of ImageView of the displayed ad image@Override Public intGetCount () {returncontent.size (); }        //ways to implement headings@Override PublicCharsequence Getpagetitle (intposition) {            returnTitle.get (position); }        //must be 2-to determine whether the display is the same picture, here we will compare two parameters to return to@Override Public Booleanisviewfromobject (View view, Object object) {returnView = =object; }        //You must 3-pageradapter only three images to be displayed, and if the sliding picture is out of range of the cache, this method will be called to destroy the image.@Override Public voidDestroyitem (ViewGroup container,intposition, Object object)            {(Viewpager) container). Removeview (Content.get (position)); //Super.destroyitem (container, Position, object);        }        //must be 4-//when the image to be displayed can be cached, this method is called to initialize the display image.//The ImageView we are going to display is added to ViewGroup and returned as a return value .@Override PublicObject Instantiateitem (ViewGroup container,intposition)            {(Viewpager) container). AddView (Content.get (position)); returnContent.get (position); }    }}



android--using Fragmentpageradapter to realize the sliding effect of fragment

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.