Android implements left-right sliding Interface

Source: Internet
Author: User

Android implements left-right sliding Interface

The first thing to do is to slide the interface and multiple views are needed for switching. In fact, you can use an ArrayList PageViews to save the view information, and then switch

 

LayoutInflater inflater = getLayoutInflater();pageViews = new ArrayList
 
  ();pageViews.add(inflater.inflate(R.layout.item01, null));pageViews.add(inflater.inflate(R.layout.item02, null));pageViews.add(inflater.inflate(R.layout.item03, null));
 

Define the xml file for each item01, item02, and item03

 

 

 

What I wrote above is a relatively simple imageview for an item,

 

Here we have to say that a class GuidPageChangeListener must be defined to inherit the OnPageChangeListener interface to implement switching between views.

 

public void onPageSelected(int arg0) {// TODO Auto-generated method stubfor (int i = 0; i < imageViews.length; i++) {imageViews[arg0].setBackgroundResource(R.drawable.page_indicator_focused);if (arg0 != i) {imageViews[i].setBackgroundResource(R.drawable.page_indicator);}}}
An adapter PagerAdapter class is also needed here. We need to write a class of our own to inherit it.

 

Class GuidPageAdapter extends PagerAdapter

The main reference method is as follows:

 

class GuidPageAdapter extends PagerAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn pageViews.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0 == arg1;}@Overridepublic void destroyItem(View arg0, int arg1, Object arg2) {// TODO Auto-generated method stub((ViewPager) arg0).removeView(pageViews.get(arg1));}@Overridepublic Object instantiateItem(View arg0, int arg1) {// TODO Auto-generated method stub((ViewPager) arg0).addView(pageViews.get(arg1));return pageViews.get(arg1);}}

In this way, you can implement a simple switching between the left and right sides of the view. During this switching, the full screen is switched.

 

Reprinted please indicate the source

Http://blog.csdn.net/pishum/article/details/38024331


 

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.