导航页面ViewPager和欢迎界面的实现

来源:互联网 发布:用u盘重装mac 编辑:程序博客网 时间:2024/06/06 10:22


在activity_welcome.xml文件中,

 

<?xml version="1.0"encoding="utf-8"?>
<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.example.yuxue.learnviewpager.WelcomeActivity"
>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/weibowelcome"
/>
</RelativeLayout>

 

WelComeActivity.class文件中

 

package com.example.yuxue.learnviewpager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompatBase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class WelcomeActivityextendsAppCompatActivity {
    private static final int SLEEP_time=5000;//欢迎界面的显示时间
    
private static final intGO_home=100;
    private static final int GO_Viewpager=101;
    private boolean isFirstIn=false;  //记录是否是第一次进入APP
    
Handler handler=newHandler(){
        @Override
        public voidhandleMessage(Message msg) {
            switch (msg.what){
                case GO_home://进入主界面
                    
Intent intenthome=newIntent(WelcomeActivity.this,MainActivity.class);
                    startActivity(intenthome);
                    WelcomeActivity.this.finish();
                    break;
                case GO_Viewpager://进入导航页面
                    
Intent intentviewpager=newIntent(WelcomeActivity.this,GuideActivity.class);
                    startActivity(intentviewpager);
                    WelcomeActivity.this.finish();
                    break;
            }
        }
    };
    @Override
    protected voidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        inits();
    }

    private void inits() {
        //SharedPreferences保存是否第一次进入App的参数
        
SharedPreferences sharedPreferences=getSharedPreferences("jike",MODE_PRIVATE);
        isFirstIn=sharedPreferences.getBoolean("isFirstIn",true);
        //判断是否是第一次进入APP
        
if(isFirstIn){
            handler.sendEmptyMessageDelayed(GO_Viewpager,SLEEP_time);
            //修改  isFirstIn的值为false
            
SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putBoolean("isFirstIn",false);
            editor.commit();

        }else {
            handler.sendEmptyMessageDelayed(GO_home,SLEEP_time);
        }
    }
}

 

 

在activity_guide.xml文件中

 

<?xml version="1.0"encoding="utf-8"?>
<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.example.yuxue.learnviewpager.GuideActivity"
>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
/>
    <!--图片滑动小圆点的布局界面-->
    
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:background="@null"
>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/point_black"
            android:id="@+id/ivpoint1"
/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/point_white"
            android:id="@+id/ivpoint2"
/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/point_white"
            android:id="@+id/ivpoint3"
/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/point_white"
            android:id="@+id/ivpoint4"
/>
    </LinearLayout>
</RelativeLayout>

 

 

GuideActivity.class文件中

 

package com.example.yuxue.learnviewpager;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;

public class GuideActivityextendsAppCompatActivity  implementsViewPager.OnPageChangeListener{
    private ViewpagerAdaperviewpagerAdaper;
    private List<View> list;
    private ViewPager viewPager;
    private Button btnstarthome;
    private ImageView []imageViews_point;
    private int [] iv_id={R.id.ivpoint1,R.id.ivpoint2,R.id.ivpoint3,R.id.ivpoint4};
    @Override
    protected voidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initViewPager();
        initImageviewPoint();


    }

    private void initImageviewPoint() {  //对点的图片进行初始化(找控件)
        
imageViews_point=newImageView[list.size()];
        for (inti=0;i<list.size();i++){
            imageViews_point[i]= (ImageView) findViewById(iv_id[i]);
        }
        Log.e("Guide","initImageviewPoint()>>>>>>>>>>");
    }


    private void initViewPager() {//配置Viewpager
        
LayoutInflater inflater=LayoutInflater.from(this);
        list=newArrayList<View>();
        list.add(inflater.inflate(R.layout.guidepager1,null));
        list.add(inflater.inflate(R.layout.guidepager2,null));
        list.add(inflater.inflate(R.layout.guidepager3,null));
        list.add(inflater.inflate(R.layout.guidepager4,null));
        viewpagerAdaper=newViewpagerAdaper(list,this);
        viewPager= (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(viewpagerAdaper);
        //找到guidepager4页面的控件按钮
        
btnstarthome= (Button)list.get(3).findViewById(R.id.btnstarthome);
        btnstarthome.setOnClickListener(newView.OnClickListener() {
            @Override
            public voidonClick(View v) {
                //跳转到主界面并杀死导航页面
                
Intent intent=newIntent(GuideActivity.this,MainActivity.class);
                startActivity(intent);
                GuideActivity.this.finish();
            }
        });
        viewPager.setOnPageChangeListener(this);   //监听ViewPager
        
Log.e("Guide","initViewPager()>>>>>>>>>>");
    }

    @Override
    //ViewPager当前新的页面被选中时调用
    
public voidonPageSelected(intposition) {
        for (inti=0;i<iv_id.length;i++){
            //ViewPager的页面的下标等于i,就把点的图标换为黑色的,否则就换为白色
            
if(position==i){
                imageViews_point[i].setImageResource(R.drawable.point_black);
            }else {
                imageViews_point[i].setImageResource(R.drawable.point_white);
            }
        }
        Log.e("Guide","onPageSelected()>>>>>>>>>>");
    }

    @Override
    //ViewPager的页面被滑动时调用
    
public voidonPageScrolled(intposition, float positionOffset,intpositionOffsetPixels) {

    }

    @Override
    //ViewPager的滑动状态改变时调用
    
public voidonPageScrollStateChanged(intstate) {

    }


    //定义一个内部类用作adaper
    
public classViewpagerAdaperextends PagerAdapter{
        private List<View>viewList;
        private Context context;

        public ViewpagerAdaper(List<View> viewList, Context context) {
            this.viewList= viewList;
            this.context= context;
        }

        @Override
        //销毁一个View
        
public voiddestroyItem(ViewGroup container,intposition, Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        //加载View
        
publicObject instantiateItem(ViewGroup container,intposition) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        //返回当前View的数量
        
public intgetCount() {
            return viewList.size();
        }

        @Override
        //判断当前的View是不是我们需要的一个对象
        
public booleanisViewFromObject(View view, Object object) {
            return (view==object);
        }
    }
}

 

 

其中guidepager1.xmlguidepager3布局都相同,除了图片不一样就只写一个了

<?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:src="@drawable/guide1"
/>
</LinearLayout>

 

guidepager4.xml文件中

 

<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"android:layout_width="match_parent"
    android:layout_height="match_parent"
>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/guide4"
/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_marginBottom="127dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="进入主页面"
            android:textSize="30dp"
            android:background="@null"
            android:id="@+id/btnstarthome"
/>
    </LinearLayout>
</RelativeLayout>

 

 

manifest文件中

由于需要布局好看,我们在welcome和引导页去掉了ActionBar

 

<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yuxue.learnviewpager"
>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
>
        <activity android:name=".MainActivity"></activity>
        <activity android:name=".GuideActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
>
        </activity>
        <activity android:name=".WelcomeActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
>
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

以上就是基本的代码了,由于MainActivity和他的布局没有添加什么东西,我就不写了,第一次进入APP时,会进入欢迎界面——>导航界面——>主界面,以后就不会再次进入导航界面了

 

 

 

 

 

 

       

 

 

 

 

 

 

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 车子自己刮花了怎么办 刚买的新车刮了怎么办 40周胎盘才一级怎么办 没感情了想离婚怎么办 对老公没感情了怎么办 和老公没感情了怎么办 跟老公没感情了怎么办 对老公感情淡了怎么办 和老公感情淡了怎么办 脖子又短又粗怎么办 18岁胸越来越小怎么办 觉得自己变丑了怎么办 我的皮肤很黑怎么办 喝老婆奶水会硬怎么办 真的很讨厌老公怎么办 老婆很讨厌我了怎么办 2岁泰迪牙齿掉了怎么办 4岁泰迪门牙掉了怎么办 种牙寿命结束后怎么办 后面的牙齿掉了怎么办 门牙齿掉了一颗怎么办 隆鼻后鼻头歪了怎么办 狗狗呕吐有虫子怎么办 狗狗呕吐出虫子怎么办 借债人无力还钱怎么办 微博被盗用了怎么办 我的眼袋很严重怎么办 怀孕了吐的很厉害怎么办 老婆怀孕吐的很厉害怎么办 孕妇6个月肚子疼怎么办 孕妇7个月肚子疼怎么办 辣椒吃多了胃疼怎么办 大腿内侧磨烂了怎么办 左脸腮帮子肿了怎么办 牙痛肿了半边脸怎么办 轮胎完全没气了怎么办 晚上车胎没气了怎么办 皇冠车胎没气了怎么办 娶了个懒老婆怎么办 办去法国的签证怎么办 别人在背后骂你怎么办