导航页面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.xml与guidepager3布局都相同,除了图片不一样就只写一个了
<?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时,会进入欢迎界面——>导航界面——>主界面,以后就不会再次进入导航界面了
- 导航页面ViewPager和欢迎界面的实现
- viewpager实现欢迎界面
- ViewPager实现应用的欢迎界面
- ViewPager之引导界面---实现欢迎引导页面
- 引导界面(二)使用ViewPager实现欢迎引导页面
- ViewPager之引导界面---实现欢迎引导页面
- ViewPager之引导界面---实现欢迎引导页面
- ViewPager实现页面切换 (包含App第一次登录时候的欢迎界面的展示)
- ViewPager的欢迎界面
- 利用viewPager实现导航界面
- ViewPager的详解(使用viewPager实现导航页面)
- ViewPager欢迎界面到引导界面的的实现之一
- 欢迎界面,自定义viewpager实现
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- 1054. 求平均值 (20)
- php 枚举问题
- java主键生成
- java源码分析(2)-String(1)
- 利用测试工具Loadrunner,刷博客访问量与积分实战
- 导航页面ViewPager和欢迎界面的实现
- android listView设置了点击事件 点击无效
- codeforces round 348 div2
- AES加密算法,JAVA实现示例
- Struts2中action 访问servlet API
- android editview 点击弹出输入法时,不把标题栏顶出窗体之外
- UISegmentedControl
- 5种服务器网络编程模型讲解
- Android平台的跨进程通信——AIDL(上)