Using Rollviewpager to implement Android scrolling banner

Source: Internet
Author: User

A rolling banner is implemented in the recent project, and the effect is generally

  

This is not convenient to write their own, and it is difficult to write to ensure that there is no bug and performance defects, fortunately, someone on the net open source of a rolling banner to achieve the Rollviewpager framework, pro-test convenient and feasible. This blog post simply records how to use the Open source framework Rollviewpager to achieve Android scrolling banner, want to learn more can go to GitHub download source code: Https://github.com/Jude95/RollViewPager

The main implementation of the results are: 1. Picture Infinite Carousel

2. Pause Carousel when touching

3. Indicators can be normal origin, numbers, and custom images

4. Implement Click events

Operation Flow:

1. Add Dependencies in Gradle (latest version see documentation on GitHub, use 1.3.2 here), then sync Gradle

  

2. Writing the layout file Activity_main.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"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Tools:context= "Edu.neu.steve.rollviewpagerdemo.MainActivity">    <Com.jude.rollviewpager.RollPagerViewAndroid:id= "@+id/roll_view_pager"Android:layout_width= "Match_parent"Android:layout_height= "210DP"App:rollviewpager_play_delay= " the"App:rollviewpager_hint_paddingbottom= "8DP"        />    </LinearLayout>

3. Writing Mainactivity.java

 PackageEdu.neu.steve.rollviewpagerdemo;ImportAndroid.graphics.Color;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast;ImportCom.jude.rollviewpager.RollPagerView;ImportCom.jude.rollviewpager.adapter.LoopPagerAdapter;ImportCom.jude.rollviewpager.hintview.ColorPointHintView; Public classMainactivityextendsappcompatactivity{PrivateRollpagerview Mrollviewpager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mrollviewpager=(Rollpagerview) Findviewbyid (R.id.roll_view_pager); Mrollviewpager.setanimationdurtion (500);//Setting the switching timeMrollviewpager.setadapter (NewTestloopadapter (Mrollviewpager));//setting up the adapterMrollviewpager.sethintview (NewColorpointhintview ( This, Color.White, Color.gray));//Set the dot indicator color//Mrollviewpager.sethintview (New Iconhintview (this, R.drawable.point_focus, R.drawable.point_normal));    }    Private classTestloopadapterextendsLooppageradapter {Private int[] IMGs ={r.drawable.img0, r.drawable.img1, R.drawable.img2, R.DRAWABLE.IMG3, R.drawable.img4,}; Private intCount =imgs.length;  PublicTestloopadapter (Rollpagerview viewpager) {Super(Viewpager); } @Override PublicView GetView (ViewGroup container,intposition) {            Final intPicno = position + 1; ImageView View=NewImageView (Container.getcontext ());            View.setimageresource (Imgs[position]);            View.setscaletype (ImageView.ScaleType.CENTER_CROP); View.setlayoutparams (Newviewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); View.setonclicklistener (NewView.onclicklistener ()//Click events{@Override Public voidOnClick (View v) {toast.maketext (mainactivity. This, "clicked on" + Picno + "Picture", Toast.length_short). Show ();            }            }); returnview; } @Override Public intGetrealcount () {returncount; }    }}

4. Import Picture Resources

The banner content in the general application is obtained from the server, and this Part I will add in later ...

Using Rollviewpager to implement Android scrolling banner

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.