1. 程式人生 > >Android之ViewPager實現引導頁(第一次開啟App進入引導頁,第二次直接進入APP主頁面)...

Android之ViewPager實現引導頁(第一次開啟App進入引導頁,第二次直接進入APP主頁面)...

有的App當你第一次開啟的是和常常會有引導頁來描述一些App資訊(功能,特點),當然也要做驗證,驗證第二次進入不進入引導頁,直接進入App,此部落格藉助ViewPager來實現引導頁, ViewPager類提供了多介面切換的新效果,是谷歌在3.0之後加入的新特性,所以需要引用android.support.v4.view.ViewPager。

效果:

程式碼:

main.xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.channeloffdemo.MainActivity">
    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#abcdef" >

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="bottom"
            android:gravity="center" >

            <View
                android:id="@+id/y1"
                android:layout_width="9dip"
                android:layout_height="9dip"
                android:layout_marginLeft="3dip"
                android:layout_marginRight="3dip"
                android:background="@drawable/y_focused" />

            <View
                android:id="@+id/y2"
                android:layout_width="9dip"
                android:layout_height="9dip"
                android:layout_marginLeft="3dip"
                android:layout_marginRight="3dip"
                android:background="@drawable/y_normal" />

            <View
                android:id="@+id/y3"
                android:layout_width="9dip"
                android:layout_height="9dip"
                android:layout_marginLeft="3dip"
                android:layout_marginRight="3dip"
                android:background="@drawable/y_normal" />
        </LinearLayout>
    </FrameLayout>

</LinearLayout>

MainActivity.Java
public class MainActivity extends FragmentActivity {

    private ViewPager viewPager;
    private List<View> listImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (ApplicationDB.preferences.getString("domeYinDao", "2").equals("1")) {
            Intent intent = new Intent(MainActivity.this, DomeActivity.class);
            startActivityForResult(intent, 1);
        } else {
            setContentView(R.layout.activity_main);
            initView();
        }


    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), showView()));
        listImg = new ArrayList<View>();
        listImg.add(findViewById(R.id.y1));
        listImg.add(findViewById(R.id.y2));
        listImg.add(findViewById(R.id.y3));
        viewPager.setOnPageChangeListener(showPageChange);
    }


    ViewPager.OnPageChangeListener showPageChange = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < listImg.size(); i++) {
                if (i == arg0) {
                    listImg.get(arg0).setBackgroundResource(R.drawable.y_focused);
                } else {
                    listImg.get(i).setBackgroundResource(R.drawable.y_normal);
                }
            }
        }

        @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

        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == 1) {
                finish();
            }
        }
    }


    private List<Fragment> showView() {
        List<Fragment> listView = new ArrayList<Fragment>();
        listView.add(new Fragment1());
        listView.add(new Fragment2());
        listView.add(new Fragment3());
        return listView;
    }
}

由於程式碼太多,完整程式碼未給出,原始碼直接下載即可