android UI用ViewPager实现引导页面

本文介绍如何利用ViewPager组件在Android应用中组织一组图像并实现流畅的滑动浏览体验,同时展示如何在最后一页添加按钮点击功能,以及如何在底部使用小圆点作为导航指示器。详细步骤包括布局设计、适配器实现、页面切换监听以及交互细节的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ViewPager主要用来组织一组数据,并且通过左右滑动的方式来展示。
最后一页实现按钮点击功能
activity_main.xml 布局界面比较简单,加入ViewPager组件,以及底部的引导小点

<RelativeLayout xmlns: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"
    tools:context="com.haitun.viewpage.MainActivity" >

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

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:contentDescription="@string/point_img"
            android:padding="15dp"
            android:src="@drawable/point" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
             android:contentDescription="@string/point_img"
            android:padding="15dp"
            android:src="@drawable/point" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:contentDescription="@string/point_img"
            android:padding="15dp"
            android:src="@drawable/point" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:contentDescription="@string/point_img"
            android:padding="15dp"
            android:src="@drawable/point" />
    </LinearLayout>

</RelativeLayout>

point.xml 小圆点
其中小点的图片用一个selector来控制颜色(设置item的enable为true或者false)

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:drawable="@drawable/point_normal" />
    <item android:state_enabled="false" android:drawable="@drawable/point_select" />
</selector>

viewpager_end.xml 最后一页需单独定义xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewPage_end"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/first">

    <Button
        android:id="@+id/viewPage_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/viewPager_access" />

</RelativeLayout>

下面就是写Activity了

package com.haitun.viewpage;

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

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnPageChangeListener {
    private ViewPager mviewPager;
    private int[] mImgIds = { R.drawable.first, R.drawable.second,
            R.drawable.three };
    private List<View> mImages = new ArrayList<View>();
    // 底部小点的图片
    private ImageView[] points;
    // 记录当前选中位置
    private int currentIndex;

    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mviewPager = (ViewPager) findViewById(R.id.viewPage);

        for (int i = 0; i < mImgIds.length; i++) {
            ImageView imageView = new ImageView(MainActivity.this);
            imageView.setImageResource(mImgIds[i]);
            imageView.setScaleType(ScaleType.CENTER_CROP);
            mImages.add(imageView);
        }
        View endView= View.inflate(this, R.layout.viewpager_end, null);
        mImages.add(endView);
        //设置适配器
        mviewPager.setAdapter(new adapter());

        // 设置监听
        mviewPager.setOnPageChangeListener(this);
        // 初始化底部小点
        initPoint();
    }

    public class adapter extends PagerAdapter {

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mImages.get(position));

        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(mImages.get(position));
            if(position>2){
                btn=(Button) findViewById(R.id.viewPage_btn);
                btn.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Log.i("end", "end");

                    }
                });
            }
            return mImages.get(position);
        }

        // 判断是否由对象生成界面
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return (arg0 == arg1);
        }

        // 返回当前有效视图的个数
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if(mImages!=null){
                return mImages.size();
            }
            return 0;
        }

    }

    public void initPoint() {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
        points = new ImageView[mImages.size()];
        // 循环取得小点图片
        for (int i = 0; i < mImages.size(); i++) {
            // 得到一个LinearLayout下面的每一个子元素
            points[i] = (ImageView) linearLayout.getChildAt(i);
            // 默认都设为灰色
            points[i].setEnabled(true);
        }
        // 设置当面默认的位置
        currentIndex = 0;
        points[currentIndex].setEnabled(false);

    }

    /**
     * 当滑动状态改变时调用
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    /**
     * 当新的页面被选中时调用
     */
    @Override
    public void onPageSelected(int positon) {
        // TODO Auto-generated method stub
        // 设置底部小点选中状态
        if (positon < 0 || positon > mImages.size() - 1
                || currentIndex == positon) {
            return;
        }
        points[positon].setEnabled(false);
        points[currentIndex].setEnabled(true);
        currentIndex = positon;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值