Android ViewPager loop Demo

Source: Internet
Author: User

Android ViewPager loop Demo
ViewPager is a software package officially provided by Google that is compatible with Android devices of earlier versions. It contains APIs only available for Android 3.0 and later versions. Viewpager is now standard. If ViewPager is not used by an App, it is still rare. Commonly Used functions of navigation and page menus are similar to those of LisstView. ListView often uses BaseAdapter, viewPager inherits PagerAdapter. For simple use, you can go to the official website to view the basic layout of the webpage. Many of them are beautiful on the Internet. I 'd like to put a TextView simply: activity_main.xml & lt; RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http: // schema S.android.com/tools "android: layout_width =" match_parent "android: layout_height =" match_parent "android: paddingBottom =" @ dimen/Taobao "android: paddingLeft =" @ dimen/Taobao "android: paddingRight = "@ dimen/activity_horizontal_margin" android: paddingTop = "@ dimen/activity_vertical_margin" tools: context = "com. example. googleviewpager. mainActivity "> <android. support. v4.view. ViewPager android: id = "@ + id/viewpager" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center"/> </RelativeLayout> three layout files to be switched, one. xml, two. xml, three. xml, one. xml Code. If the other two are similar, no code will be pasted: <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: text = "Zhou Yongkang opened" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> </LinearLayout> the Demo implements the code in onCreate, in fact, three views are loaded separately, and the first and last views are loaded with an empty View, mainly to enable the Left and Right loops: setContentView (R. layout. activity_main); LayoutInflater inflater = getLayoutInflater (); viewPager = (ViewPager) findViewById (R. id. viewpager); view1 = inflater. inflate (R. layout. one, null); view1.setBackgroundColor (Color. RED); view2 = inflater. inflate (R. layout. two, null); view2.setBackgroundColor (Color. BLACK); view3 = inflater. inflate (R. layout. three, null); view3.setBackgroundColor (Color. BLUE); viewList = new ArrayList <View> (); viewList. add (new View (this); viewList. add (view1); viewList. add (view2); viewList. add (view3); viewList. add (new View (this); viewPager. setAdapter (new MyViewPagerAdapter (viewList); viewPager. setCurrentItem (1); custom MyViewPagerAdapter: private class MyViewPagerAdapter extends PagerAdapter {@ Override public void destroyItem (ViewGroup container, int position, Object object Object) {// TODO Auto-generated method stub container. removeView (myList. get (position) ;}@ Override public Object instantiateItem (ViewGroup container, int position) {// TODO Auto-generated method stub container. addView (myList. get (position), 0); return myList. get (position);} private List <View> myList; public MyViewPagerAdapter (List <View> list) {myList = list;} @ Override public int getCount () {// TODO Auto-generated method stub return myList. size () ;}@ Override public boolean isViewFromObject (View arg0, Object arg1) {// TODO Auto-generated method stub return arg0 == (arg1 );}} after writing the above, you still need to set the setOnPageChangeListener page event. The focus of the loop is to switch to an empty View and replace the page with viewPager. setOnPageChangeListener (new OnPageChangeListener () {@ Override public void onPageSelected (int arg0) {// TODO Auto-generated method stub System. out. println ("onPageSelected =" + arg0); if (arg0 = 0) viewPager. setCurrentItem (viewList. size ()-2); else if (arg0 = viewList. size ()-1) viewPager. setCurrentItem (1) ;}@ Override public void onPageScrolled (int arg0, float arg1, int arg2) {// TODO Auto-generated method stub} @ Override public void merge (int arg0) {// TODO Auto-generated method stub}

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.