使用ViewPager實現android軟體使用嚮導功能實現步驟_Android

來源:互聯網
上載者:User



首先需要一個布局檔案,是FlameLayout組成的,裡麵包含了一個ViewPager和一個RelativeLayout,RelativeLayout裡面是一個LinearLayout,LinearLayout裡面是準備放ImageView,動態添加。

布局檔案如下:

複製代碼 代碼如下:

<?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/guidePages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">
        <LinearLayout
            android:id="@+id/viewGroup"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_marginBottom="30dip">

        </LinearLayout>
    </RelativeLayout>
</FrameLayout>

另外我們需要4個布局檔案,就是嚮導要顯示的圖片,每個布局檔案是一頁,每個布局檔案裡面都是一個ImageView。如下所示:

布局檔案一:

複製代碼 代碼如下:

<?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:orientation="vertical" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/feature_guide_0"/>"

</LinearLayout>

布局檔案二:

複製代碼 代碼如下:

<?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:orientation="vertical" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/feature_guide_1"/>

</LinearLayout>

布局檔案三:

複製代碼 代碼如下:

<?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:orientation="vertical" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/feature_guide_2"/>"

</LinearLayout>

布局檔案四:

複製代碼 代碼如下:

<?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:orientation="vertical" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/feature_guide_3"/>"

</LinearLayout>

然後在代碼裡面載入這4個布局檔案和主布局檔案:

Activity代碼:

複製代碼 代碼如下:

package com.alex.helloworld;

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.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class HelloWord2 extends Activity implements OnPageChangeListener {

    private ArrayList<View> mPageViews;
    private LayoutInflater mInflater;
    private LinearLayout mGroups;
    private FrameLayout mMain;
    private ViewPager mViewPager;
    private ImageView[] mImageViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mInflater = LayoutInflater.from(this);
        mPageViews = new ArrayList<View>();
        mPageViews.add(mInflater.inflate(R.layout.item_0, null));
        mPageViews.add(mInflater.inflate(R.layout.item_1, null));
        mPageViews.add(mInflater.inflate(R.layout.item_2, null));
        mPageViews.add(mInflater.inflate(R.layout.item_3, null));

        mMain = (FrameLayout) mInflater.inflate(R.layout.hello2, null);
        mGroups = (LinearLayout) mMain.findViewById(R.id.viewGroup);
        mViewPager = (ViewPager) mMain.findViewById(R.id.guidePages);
        mImageViews = new ImageView[mPageViews.size()];

        for(int i=0; i<mPageViews.size(); i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(new LayoutParams(20, 20));
            mImageViews[i] = iv;
            if(i == 0) {
                mImageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                mImageViews[i].setBackgroundResource(R.drawable.page_indicator);
            }
            mGroups.addView(mImageViews[i]);
        }
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setOnPageChangeListener(this);
        setContentView(mMain);
    }

    private PagerAdapter mPageAdapter = new PagerAdapter() {

        @Override
        public void startUpdate(View arg0) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager)arg0).addView(mPageViews.get(arg1));
            return mPageViews.get(arg1);
        }

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

        @Override
        public void finishUpdate(View arg0) {

        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager)arg0).removeView(mPageViews.get(arg1));
        }
    };

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for(int i=0; i<mPageViews.size(); i++) {
            mImageViews[position].setBackgroundResource(R.drawable.page_indicator_focused);
            if(position != i) {
                mImageViews[i].setBackgroundResource(R.drawable.page_indicator);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}

首先從LayoutInflater裡面載入4個要顯示的布局和主布局檔案。

然後把布局檔案作為View放到一個ArrayList裡面。

然後從主布局裡面找到ViewPager和LinearLayout,ViewPager用來裝載4個布局檔案,LinearLayout用來裝載4個提示表徵圖。

然後建立4個ImageView,並設定對應的背景,然後作為View添加到LinearLayout裡面去。

然後給ViewPager設定Adapter,設定onPageChangeListener。

Adapter裡面要設定getCount,就是頁面的個數,我們這裡是4個,就設定4;

同時在instantiateItem裡面講對應的頁面add進去,並返回對應的頁面。

在destroyItem的時候講頁面remove掉。

在選擇頁面的方法裡面onPageSelected裡面設定選中表徵圖的背景。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.