Android开发之使用ViewPager做引导页面(转载)

转载自:deng0zhaotai http://blog.csdn.net/deng0zhaotai/article/details/24744637

MainActivity.java

package com.example.viewpagerdemo;

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.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ViewPager viewPager = null;
    private ImageView img1, img2, img3;
    private ArrayList<String> titles;
    private int curIndex = -1;
    private Button before,next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        before=(Button)findViewById(R.id.before);
        next=(Button)findViewById(R.id.next);
        initWidgets();
        // 把要显示的View装入数组
        LayoutInflater li = LayoutInflater.from(this);
        View view1 = li.inflate(R.layout.pager1, null);
        View view2 = li.inflate(R.layout.pager2, null);
        View view3 = li.inflate(R.layout.pager3, null);

        // 添加页面
        final ArrayList<View> views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);
        views.add(view3);

        // 添加标题
        titles = new ArrayList<String>();
        titles.add("tab1");
        titles.add("tab2");
        titles.add("tab3");

        picViewPagerAdapter pagerAdapter = new picViewPagerAdapter(views);
        viewPager.setAdapter(pagerAdapter);
        //打开程序显示的当前页面
        curIndex = 0;
        viewPager.setCurrentItem(curIndex);
        //imag1为选中的点,且上按钮不显示
        img1.setImageResource(R.drawable.page_icon_sel);
        before.setVisibility(View.INVISIBLE);
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                switch (arg0) {
                case 0:
                    before.setVisibility(View.INVISIBLE);
                    next.setVisibility(View.VISIBLE);
                    img1.setImageResource(R.drawable.page_icon_sel);
                    img2.setImageResource(R.drawable.page_icon);
                    img3.setImageResource(R.drawable.page_icon);
                    break;
                case 1:
                    before.setVisibility(View.VISIBLE);
                    next.setVisibility(View.VISIBLE);
                    img2.setImageResource(R.drawable.page_icon_sel);
                    img1.setImageResource(R.drawable.page_icon);
                    img3.setImageResource(R.drawable.page_icon);
                    break;
                case 2:
                    before.setVisibility(View.VISIBLE);
                    next.setVisibility(View.INVISIBLE);
                    img3.setImageResource(R.drawable.page_icon_sel);
                    img1.setImageResource(R.drawable.page_icon);
                    img2.setImageResource(R.drawable.page_icon);

                    break;
                default:
                    break;
                }
                curIndex = arg0;
                System.out.println("[MainActivity->]currIndex = " + curIndex);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

        before.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(curIndex!=0)
                viewPager.setCurrentItem(--curIndex);

            }
        });
       next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(curIndex!=2)
                viewPager.setCurrentItem(++curIndex);
            }
        });
    }

    private void initWidgets() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        img1 = (ImageView) findViewById(R.id.icon_1);
        img2 = (ImageView) findViewById(R.id.icon_2);
        img3 = (ImageView) findViewById(R.id.icon_3);

    }

    /**
     * 为ViewPager添加适配器
     *
     * @author Administrator
     *
     */
    class picViewPagerAdapter extends PagerAdapter {

        private List<View> listViews;

        public picViewPagerAdapter(List<View> list) {
            listViews = list;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(listViews.get(position));
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(listViews.get(position));
            return listViews.get(position);
        }
    }
}

activity_main.xml

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

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/icon_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:contentDescription="@string/content_description"
            android:src="@drawable/page_icon" />

        <ImageView
            android:id="@+id/icon_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:contentDescription="@string/content_description"
            android:src="@drawable/page_icon" />

        <ImageView
            android:id="@+id/icon_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/content_description"
            android:src="@drawable/page_icon" />
    </LinearLayout>

    <Button
        android:id="@+id/before"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一页" />

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一页"
        android:layout_gravity="right" />

</FrameLayout>

pager1.xml

<?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:background="@drawable/image1"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_in_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="第一页" />

</LinearLayout>

pager2.xml

<?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:background="@drawable/image2"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_in_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="第二页" />

</LinearLayout>

pager3.xml

<?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:background="@drawable/image3"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_in_third"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="第三页" />

</LinearLayout>

strings.xm

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">viewpagerdemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="content_description">test</string>

</resources>

素材:

时间: 2024-08-09 19:28:17

Android开发之使用ViewPager做引导页面(转载)的相关文章

Android开发之使用ViewPager做引导页面

引导页面相信大家都不会陌生,安装了一个新的App后第一次打开,都会有类似下图,相当于说明文档 实现效果 程序目录结构 在主layout里main.xml定义一个帧布局,在viewPager上有多少页就显示多少个点 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

【Android开发-1】必做的事情,android开发环境搭建

前言:互联网移动端现在发展的速度越来越快了,各种App应用软件和手游的崛起,把移动端弄的丰富多彩:心情一激动,头脑一发热,就开始看视频.看书学习了.记得11年的时候有学习了下Android的开发,但是只是基础的学习了下,没认真深入学习.这次自己决定认真学习下,并用博客记录下,见证自己成长的过程. 一.准备工作 1.下载最新版jdk:http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.下载Eclipse,选择Ec

50个Android开发人员必备UI效果源码[转载]

50个Android开发人员必备UI效果源码[转载] http://blog.csdn.net/qq1059458376/article/details/8145497 Android 仿微信之主页面实现篇Android 仿微信之界面导航篇Android 高仿QQ 好友分组列表Android 高仿QQ 界面滑动效果Android 高仿QQ 登陆界面Android 对Path的旋转效果的拓展Android高仿360安全卫士布局源码Android SlidingDrawer 滑动抽屉效果Androi

Android AnimationDrawable动画与APP启动引导页面

Android AnimationDrawable动画与APP启动.加载引导页面(画面) AnimationDrawable是Android的Frame动画,可以简单的认为此AnimationDrawable能够将一系列资源图片加载成"电影"一样播放.当下,在一些APP中,往往需要在APP切入主题之前加载一些引导页面(宣传海报.装饰画面.赞助商广告等等),这些内容要两个共同特点: (1)不是APP的重点内容,而仅仅只是像展台一样展览一些画面. (2)前.当前.后页面基本上无特别需要处理

Android开发实战之ViewPager的轮播

在安卓开发的许多控件中,如果你没有使用过ViewPager,就不能算是一个安卓开发工程师,在本篇博文中,我会总结ViewPager的使用方法, 以及一些开发中的拓展.希望本篇博文对你的学习和工作有所帮助. **ViewPager的基本使用** ViewPager的使用遵循MVC模式,M(模型),V(视图),C(控制器).模型就是viewpager对象,视图就是xml视图,控制器就是适配器adapter.所以, 要实现一个完整的ViewPager这三个东西一个都不能少. xml: <?xml ve

android开发之定制ViewPager滑动事件

明天还要加班,苦逼的程序猿,简单说说最近遇到的一个问题吧. 我在viewpager+fragment学习笔记中简单介绍过ViewPager+Fragment的用法,其实并不难,当时实现了一个如下图所示的效果: 然而,在实际开发中这一点技术可能根本不够用,项目中会有各种各样奇葩的需求,我最近就遇到了一个怪异的需求,捣鼓之后还是解决了,今天和大家聊聊. 由于涉及到公司项目,我在这里就使用我自己制作的一个Demo来和大家介绍. 我们要实现的效果如下图: 这里一共有三个Fragment,其中两个通过左右

android开发之提高应用启动速度_splash页面瞬间响应_避免APP启动闪白屏

Application和Activity中的onCreate都进行了优化,基本没有耗时操作,但是启动应用之后还是会闪现一下白色背景,然后才进入Splash页面,对比了一下QQ.微信.微博等客户端,点击之后都是瞬间响应Splash启动页,差别在哪里呢. 其实就算你onCreate啥都不做,仍然会闪一下白屏,因为初始化解析界面时需要一定时间,解决方法是自定义Theme. 自定义如下 <style name="AppSplash" parent="android:Theme&

Android开发中用友盟做分享的一些坑

仅限于用5.1.4版本的 按照友盟分享的API在自己的代码中修改: 1.微信分享需要打包APK文件,数字签名与微信开发申请的要一致 2.此name中属性不能修改 value为友盟的申请的appkey <meta-data   android:name="UMENG_MESSAGE_SECRET"    android:value="******************************" > 3.做微博分享时:libs里面添加SocialSDK_S

android开发(2):多页面的实现 | Fragment的创建与使用

APP中出现多个页面再常见不过了.使用activity与fragment都能实现多页面,这里使用fragment来实现.延续"知音"这个APP的开发,之前已经创建了底部导航条与mainactivity. 首先创建一个fragment,作为"广场"页面.在菜单中new一个fragment出来即可,如下面的截图: 此时会自动生成这个fragment的xml跟类,xml用于界面设计,而fragment类用于界面管理,并被activity类使用,请看截图: 注意,Fragm