ViewPager實現引導頁(添加導航點,判斷是否第一次進入主介面),viewpager主介面

來源:互聯網
上載者:User

ViewPager實現引導頁(添加導航點,判斷是否第一次進入主介面),viewpager主介面

1.引導頁的4個介面布局,裡面載入一張背景圖片

插入到guide的介面布局中(這裡不用fragment)

guide_background_fragment1.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"    ><ImageView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/guide_background1"/></LinearLayout>

guide_background_fragment2.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"    ><ImageView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/guide_background2"/></LinearLayout>

guide_background_fragment3.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"    ><ImageView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/guide_background3"/></LinearLayout>

 

guide_background_fragment4.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"    ><ImageView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/guide_background4"/></LinearLayout>

guide.xml

<?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/viewpager"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" />    <LinearLayout        android:id="@+id/l1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_centerInParent="true"        android:layout_alignParentBottom="true"        >        <ImageView            android:id="@+id/imageV1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/login_point" />        <ImageView            android:id="@+id/imageV2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/login_point" />        <ImageView            android:id="@+id/imageV3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/login_point" />        <ImageView            android:id="@+id/imageV4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/login_point" />    </LinearLayout></RelativeLayout>

2.    適配器  

GuidePagerAdapter.java

import android.content.Context;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import java.util.List;public class GuidePagerAdapter extends PagerAdapter{    private Context context;    private List<View> views;    public GuidePagerAdapter(List<View> views, Context context) {        this.context = context;        this.views = views;    }    @Override    public int getCount() {        // TODO Auto-generated method stub        return views.size();    }    @Override    public Object instantiateItem(ViewGroup container, int position) {        container.addView(views.get(position));        return views.get(position);    }    @Override    public void destroyItem(ViewGroup container, int position, Object object) {        container.removeView(views.get(position));    }    @Override    public boolean isViewFromObject(View arg0, Object arg1) {        // TODO Auto-generated method stub        return (arg0 == arg1);    }}

Guide.java

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.ImageView;import java.util.ArrayList;import java.util.List;/** * Created by zps on 2015/9/3. */public class Guide extends Activity implements ViewPager.OnPageChangeListener {    private List<View> listView;    private ViewPager viewPager;    private GuidePagerAdapter vpAdapter;    private int[] dotsId = {R.id.imageV1, R.id.imageV2, R.id.imageV3, R.id.imageV4};    private ImageView[] dotsView;    private Button lijitiyanButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.view_pager);        init();        initDots();    }    private void initDots() {        dotsView = new ImageView[listView.size()];        for (int i = 0; i < listView.size(); i++) {            dotsView[i] = (ImageView) findViewById(dotsId[i]);        }    }    private void init() {        LayoutInflater inflater = LayoutInflater.from(this);        listView = new ArrayList<View>();        //擷取View對象        View view1 = inflater.inflate(R.layout.view_pager_img1, null);        View view2 = inflater.inflate(R.layout.view_pager_img2, null);        View view3 = inflater.inflate(R.layout.view_pager_img3, null);        View view4 = inflater.inflate(R.layout.view_pager_img4, null);        //view對象放在List<view>中        listView.add(view1);        listView.add(view2);        listView.add(view3);        listView.add(view4);        //List<view>放在適配器ViewPagerAdapter中        vpAdapter = new GuidePagerAdapter(listView, this);        //擷取ViewPage,設定適配器;        viewPager = (ViewPager) findViewById(R.id.viewpager);        viewPager.setAdapter(vpAdapter);        viewPager.addOnPageChangeListener(this);        lijitiyanButton =(Button) view4.findViewById(R.id.lijitiyan_button);        lijitiyanButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(Guide.this,MainActivity.class);                startActivity(intent);                finish();            }        });    }    @Override    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {    }    @Override    public void onPageSelected(int position) {        for (int i = 0; i < dotsId.length; i++) {            if(i == position){                dotsView[i].setImageResource(R.drawable.login_point_selected);            }else {                dotsView[i].setImageResource(R.drawable.login_point);            }        }    }    @Override    public void onPageScrollStateChanged(int state) {    }}

3.引導頁的判斷:是否第一次進入

public class GuideWelcome extends Activity{    private static final int TIME = 2000;    private static boolean isFirstIn = false;    private static final  int First = 1000;    private static final  int NoFirst = 1001;    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            switch (msg.what){                case First:                    FirstIn();                    break;                case NoFirst:                    NoFirstIn();                    break;                default:                    break;            }        }    };    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.welcome);        init();    }    private void init() {        //隱藏檔名為"gudiewelcome"        SharedPreferences sharedPreferences = getSharedPreferences("guidewelcome",MODE_PRIVATE);        isFirstIn = sharedPreferences.getBoolean("isFirstIn",true);        if(!isFirstIn){          handler.sendEmptyMessageDelayed(NoFirst,TIME);        }else {            handler.sendEmptyMessageDelayed(First,TIME);            //隱藏檔key為isFirstIn,值為false            SharedPreferences.Editor editor = sharedPreferences.edit();            editor.putBoolean("isFirstIn",false);            editor.commit();        }    }    private void FirstIn(){        Intent intent = new Intent(GuideWelcome.this,Guide.class);        startActivity(intent);        finish();    }    private void NoFirstIn(){        Intent intent = new Intent(GuideWelcome.this,TabMainActivity.class);        startActivity(intent);        finish();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.