Android custom Control 5--Viewpager Basic implementation of the Carousel map advertisement

Source: Internet
Author: User

This article address: http://www.cnblogs.com/wuyudong/p/5918021.html, reprint please indicate source address.

This article began to implement the Carousel map campaign, this article first to make the picture slide up (viewpager), the effect is as follows:

First implement the layout

<Relativelayoutxmlns: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"Tools:context=". Mainactivity " >    <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "160DP" >        <Android.support.v4.view.ViewPagerAndroid:id= "@+id/viewpager"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" />        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "40DP"Android:layout_alignparentbottom= "true"Android:background= "#66000000"android:gravity= "Center_horizontal"android:orientation= "vertical"android:padding= "5DP" >            <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:singleline= "true"Android:text= "Harmonious society, harmonious Society, harmonious society, harmonious society"Android:textcolor= "@android: Color/white" />            <LinearLayoutAndroid:id= "@+id/ll_point_container"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "5DP"android:orientation= "Horizontal" >            </LinearLayout>        </LinearLayout>    </Relativelayout></Relativelayout>

The following error occurred while running the project:

09-28 09:08:44.032:e/androidruntime (5794): java.lang.RuntimeException:Unable to start activity componentinfo{ Com.wuyudong.viewpager/com.wuyudong.viewpager.mainactivity}: Android.view.InflateException:Binary XML file Line #11 : Error Inflating class Android.support.v4.view.ViewPager

Viewpager is a great, powerful control that many apps use to achieve cool results, but in many cases you run into error inflating class Android.support.v4.view.ViewPager This question,

The solution to this problem is simple: Right-click Project selection Build Path - Configure Build Path , and then select Order and Export options. Sticky Android-support-v4.jar and check this option, and then Clean under the project, the recompilation problem is solved ~

Then write the logic code:

 PackageCom.wuyudong.viewpager;Importjava.util.ArrayList;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {PrivateViewpager Viewpager; Private int[] imageresids; PrivateArraylist<imageview>imageviewlist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Initialize Layout view Viewinitviews (); //Model DataInitData (); //controller ControllersInitadapter (); }    Private voidinitviews () {Viewpager=(Viewpager) Findviewbyid (R.id.viewpager); Imageresids=New int[] {r.drawable.a, r.drawable.b, r.drawable.c, R.DRAWABLE.D, R.DRAWABLE.E}; Imageviewlist=NewArraylist<imageview>();        ImageView ImageView;  for(inti = 0; i < imageresids.length; i++) {ImageView=NewImageView ( This);            Imageview.setbackgroundresource (Imageresids[i]);        Imageviewlist.add (ImageView); }    }    Private voidInitadapter () {//    }    Private voidInitData () {//initialize the data to be displayedViewpager.setadapter (NewMyadapter ()); }    classMyadapterextendsPageradapter {@Override Public intGetCount () {returnimageviewlist.size (); }        //3. The judgment logic of the specified multiplexing@Override Public Booleanisviewfromobject (View view, Object object) {//when you slide to a new entry and return to it, the view can be reused .            returnView = =object; }        //1. Return the contents of the entry to be displayed, create an entry@Override PublicObject Instantiateitem (ViewGroup container,intposition) {            //container: Container: Viewpager//Position: The current location of the entry to be displayedImageView ImageView =Imageviewlist.get (position); //A. Add the View object to the containerContainer.addview (ImageView); //B. Return the view object to the frame, adapter            returnImageView;//must be overridden, otherwise thrown exception        }        //2. Destruction of Entries@Override Public voidDestroyitem (ViewGroup container,intposition, Object object) {            //object to destroyContainer.removeview ((View) object); }    }}

After running the project, the effect is as follows:

Android custom Control 5--Viewpager Basic implementation of the Carousel map advertisement

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.