«

Android中ViewPager的使用

时间:2024-3-2 18:52     作者:韩俊     分类: Android


我们在一个apk中第一次开始的时候,会有一个guide界面,一般使用ViewPager来完成。

布局文件如下:

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

    <android.support.v4.view.ViewPager
        android:id="@+id/guide_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

    <Button
        android:id="@+id/guide_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:text="开始体验"
        android:textSize="20sp"
        android:visibility="gone" />

</RelativeLayout>

这就是一个想到Activity GuideActivity.java

package com.xlg.news;

import java.util.ArrayList;
import java.util.List;

import com.xlg.news.adapters.WelcomeViewPagerAdapter;
import com.xuliugen.newsclient.R;

import android.app.Activity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class GuideActivity extends Activity {

    private ViewPager guideViewPager;
    private Button guideButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_activity);
        guideViewPager = (ViewPager) this.findViewById(R.id.guide_viewpager);
        guideButton = (Button) this.findViewById(R.id.guide_button);

        //获取图片ImageView资源
        final List<ImageView> imageViewsList = initData();

        WelcomeViewPagerAdapter viewPagerAdapter = new WelcomeViewPagerAdapter(GuideActivity.this,imageViewsList);
        guideViewPager.setAdapter(viewPagerAdapter);

        /**
         * 设置guide ViewPager滑动的时候的监听事件
         */
        guideViewPager.setOnPageChangeListener(new OnPageChangeListener() {

            /**
             * 已经选中某一页的时候
             */
            @Override 
            public void onPageSelected(int position) {
                //已经到了最后一页
                if(position == imageViewsList.size()-1){
                    guideButton.setVisibility(View.VISIBLE);
                    guideButton.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            //进入最后一页设置向导页,点击按钮完成功能
                        }
                    });
                }else{
                    guideButton.setVisibility(View.GONE);
                }
            }

            /**
             * 
             * 在页面的滑动过程中
             */
            @Override
            public void onPageScrolled(int position, float arg1, int arg2) {

            }

            /**
             * 
             * 滑动状态发生改变的状态
             */
            @Override
            public void onPageScrollStateChanged(int position) {

            }
        });
    }

    private List<ImageView> initData() {
        List<ImageView> imageViewsList = new ArrayList<ImageView>();
        // 添加图片控件
        ImageView imageView1 = new ImageView(getApplicationContext());
        imageView1.setBackgroundResource(R.drawable.guide_image1);
        ImageView imageView2 = new ImageView(getApplicationContext());
        imageView2.setBackgroundResource(R.drawable.guide_image2);
        ImageView imageView3 = new ImageView(getApplicationContext());
        imageView3.setBackgroundResource(R.drawable.guide_image3);
        imageViewsList.add(imageView1);
        imageViewsList.add(imageView2);
        imageViewsList.add(imageView3);
        return imageViewsList;
    }

}

其中的适配器如下:

package com.xlg.news.adapters;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * 第一次启动页面的ViewPager适配器
 * @author xuliugen
 */
public class WelcomeViewPagerAdapter extends PagerAdapter {

    private Context mContext;
    private List<ImageView> mImageViewsList;

    public WelcomeViewPagerAdapter(Context context,
            List<ImageView> imageViewsList) {
        mContext = context;
        mImageViewsList = imageViewsList;
    }

    @Override
    public int getCount() {
        return mImageViewsList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 给制定的viewpager添加一个view的方法
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //添加一个imageView
        container.addView(mImageViewsList.get(position));
        return mImageViewsList.get(position);
    }

    /**
     * 给制定的viewpager移除一个view的方法
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}

标签: android

热门推荐