Android4.0扩展库实现左右滑动效果(一)

系统 1616 0

  在手机的桌面实现三屏页面滑动显示方式实现如下:

Android4.0扩展库实现左右滑动效果(一)

 

注意长方形中的图表变化

 

 

 

 

 

 

主要的布局文件如下:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/guidePages"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
       
    <RelativeLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="vertical" >
    <LinearLayout  
        android:id="@+id/viewGroup"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:layout_marginBottom="30dp"  
        android:gravity="center_horizontal"  
        android:orientation="horizontal" >  
    </LinearLayout>  
    </RelativeLayout>

</FrameLayout>
  

 

 

主要的代码文件如下:

    package com.easyway.guide;

import android.app.Activity;
import android.os.Bundle;


import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
/**
 * 使用Android-suport-v4.jar中android的扩展功能ViewPager实现左右滑动的切换的功能
 * 此种功能在智能机的桌面应用中多屏信息的展示中使用
 * 
 * @Title: 
 * @Description: 实现TODO
 * @Copyright:Copyright (c) 2011
 * @Company:易程科技股份有限公司
 * @Date:2012-7-16
 * @author  longgangbai
 * @version 1.0
 */
public class AndroidGuideViewActivityActivity extends Activity {
		
	    private ViewPager viewPager;  
	    private ArrayList<View> pageViews;  
	    private ViewGroup main, group;  
	    private ImageView imageView;  
	    private ImageView[] imageViews; 
		
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	        //获取页面的布局
	        LayoutInflater inflater = getLayoutInflater();  
	        pageViews = new ArrayList<View>();  
	        pageViews.add(inflater.inflate(R.layout.list_item0, null));  
	        pageViews.add(inflater.inflate(R.layout.list_item1, null));  
	        pageViews.add(inflater.inflate(R.layout.list_item2, null));  
	        pageViews.add(inflater.inflate(R.layout.list_item3, null));   
	  
	        imageViews = new ImageView[pageViews.size()];
	        //获取相关的容器
	        main = (ViewGroup)inflater.inflate(R.layout.main, null);  
	        
	        // group是R.layou.main中的负责包裹小圆点的LinearLayout.  
	        group = (ViewGroup)main.findViewById(R.id.viewGroup);  
	       //获取ViewPager对象
	        viewPager = (ViewPager)main.findViewById(R.id.guidePages);  
	  
	        //创建相关的ImageView对象并添加对象
	        for (int i = 0; i < pageViews.size(); i++) {  
	            imageView = new ImageView(AndroidGuideViewActivityActivity.this);  
	            imageView.setLayoutParams(new LayoutParams(20,20));  
	            imageView.setPadding(20, 0, 20, 0);  
	            imageViews[i] = imageView;  
	            if (i == 0) {  
	                //默认选中第一张图片
	                imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);  
	            } else {  
	                imageViews[i].setBackgroundResource(R.drawable.page_indicator);  
	            }  
	            group.addView(imageViews[i]);  
	        }  
	        //设置布局
	        setContentView(main);  
	       //设置相关的适配器对象
	        viewPager.setAdapter(new GuidePageAdapter());  
	        //设置相关的响应事件
	        viewPager.setOnPageChangeListener(new GuidePageChangeListener());  
	    }
	    
	    /** 指引页面Adapter */
	    class GuidePageAdapter extends PagerAdapter {  
	    	  
	        @Override  
	        public int getCount() {  
	            return pageViews.size();  
	        }  
	  
	        @Override  
	        public boolean isViewFromObject(View arg0, Object arg1) {  
	            return arg0 == arg1;  
	        }  
	  
	        @Override  
	        public int getItemPosition(Object object) {  
	            // TODO Auto-generated method stub  
	            return super.getItemPosition(object);  
	        }  
	  
	        @Override  
	        public void destroyItem(View arg0, int arg1, Object arg2) {  
	            // TODO Auto-generated method stub  
	            ((ViewPager) arg0).removeView(pageViews.get(arg1));  
	        }  
	  
	        @Override  
	        public Object instantiateItem(View arg0, int arg1) {  
	            // TODO Auto-generated method stub  
	            ((ViewPager) arg0).addView(pageViews.get(arg1));  
	            return pageViews.get(arg1);  
	        }  
	  
	        @Override  
	        public void restoreState(Parcelable arg0, ClassLoader arg1) {  
	            // TODO Auto-generated method stub  
	  
	        }  
	  
	        @Override  
	        public Parcelable saveState() {  
	            // TODO Auto-generated method stub  
	            return null;  
	        }  
	  
	        @Override  
	        public void startUpdate(View arg0) {  
	            // TODO Auto-generated method stub  
	  
	        }  
	  
	        @Override  
	        public void finishUpdate(View arg0) {  
	            // TODO Auto-generated method stub  
	  
	        }  
	    } 
	    
	    /** 指引页面改监听器 */
	    class GuidePageChangeListener implements OnPageChangeListener {  
	  
	        @Override  
	        public void onPageScrollStateChanged(int arg0) {  
	            // TODO Auto-generated method stub  
	  
	        }  
	  
	        @Override  
	        public void onPageScrolled(int arg0, float arg1, int arg2) {  
	            // TODO Auto-generated method stub  
	  
	        }  
	  
	        @Override  
	        public void onPageSelected(int arg0) {  
	            for (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);  
	                }  
	            }
	  
	        }  
	  
	    }  
    
}
  

 

Android4.0扩展库实现左右滑动效果(一)


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论