ViewPager实现左右滑动 在底部增加导航点 最后一个页面进入按钮进入主页面
Java文件
Guide
package com.example.lenovo.viewpager1;
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.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;
//点的改变根据viewPager中的view来改变,所以设置监听事件来监听viewPager的改变
public class Guide extends Activity implements ViewPager.OnPageChangeListener {
private ViewPager vp;
private ViewPagerAdapter vpAdapter;
private List<View> views;
private ImageView[] dots;//点的集合
private int[] ids={R.id.iv1,R.id.iv2,R.id.iv3};//位置
private Button start;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);//加载当前视图
intviews();
initDots();
}
private void intviews(){
LayoutInflater inflater=LayoutInflater.from(this);
views=new ArrayList<View>();
views.add(inflater.inflate(R.layout.one,null));
views.add(inflater.inflate(R.layout.two,null));
views.add(inflater.inflate(R.layout.three,null));
vpAdapter=new ViewPagerAdapter(views,this);
vp=findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);//绑定Adapter
start=views.get(2).findViewById(R.id.start);//初始化view时寻找id
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(Guide.this,MainActivity.class);
startActivity(i);
finish();
}
});
vp.setOnPageChangeListener(this);//回调
}
//初始化点
private void initDots(){
dots=new ImageView[views.size()];//实例化点
for(int i=0;i<views.size();i++){
dots[i]=(ImageView)findViewById(ids[i]);
}
}
//滑动状态改变时进行调用
public void onPageScrollStateChanged(int arg0) {
}
//页面被滑动时调用
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//当前新的页面被选中时调用
public void onPageSelected(int arg0) {
//用循环来更改当前点的状态
for(int i=0;i<ids.length;i++){
if(arg0==i){
dots[i].setImageResource(R.drawable.on);//设置当前为亮
}
else {
dots[i].setImageResource(R.drawable.off);//设置当前为暗
}
}
}
}
MainActivity
package com.example.lenovo.viewpager1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ViewPagerAdapter
package com.example.lenovo.viewpager1;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import java.util.List;
public class ViewPagerAdapter extends PagerAdapter{
private List<View> views;//创建集合承载所有的views
private Context context;//创建上下文
//构造函数
public ViewPagerAdapter(List<View> views, Context context){
this.views=views;
this.context=context;
}
//销毁view
public void destroyItem(View container,int position,Object object){
((ViewPager)container).removeView(views.get(position));
}
//加载view
public Object instantiateItem(View container,int position){
((ViewPager)container).addView(views.get(position));
return views.get(position);
}
//返回当前view的数量
public int getCount() {
return views.size();
}
//判断当前view是否为需要的对象
public boolean isViewFromObject(View arg0, Object arg1) {
return (arg0==arg1);
}
}
xml文件
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovo.viewpager1.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
guide
<?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="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
>
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="bottom|center">
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/on" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv2"
android:src="@drawable/off"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv3"
android:src="@drawable/off"
/>
</LinearLayout>
</RelativeLayout >
one
<?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="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/guide_1"
/>
</LinearLayout>
two
<?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="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/guide_2"
/>
</LinearLayout>
three
<?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">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/guide_3"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom|center" >
</LinearLayout>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="进入" />
</RelativeLayout>
效果图