activity.xml:
<?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=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
在创建三个引导页面,guide_one,guide_two,guide_three:
guide_one.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"
android:background="@drawable/ic_shenone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一"
android:textSize="60dp"
android:layout_centerInParent="true"
android:textColor="#03fa72"/>
</RelativeLayout>
guide_two和one时一样的,只是导入图片不同而已,重点在第三个 guide_three
gudie_three.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:background="@drawable/ic_shenthree">
<Button
android:id="@+id/btn_queding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="立即体验"
android:textSize="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
android:background="#07e4f4"
android:textColor="#ffffff"/>
</RelativeLayout>
activity_guide.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".GuideActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!"
android:textSize="30dp"/>
</RelativeLayout>
GuideActivity.java 不变,我这里就不列举了!
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private View viewOne,viewTwo,viewThree; // 三个滑动界面对应三个 view 对象
private ArrayList<View> viewArrayList = null;
private MyPagerAdapter adapter = null;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewpager);
/** 获取三个布局文件,生成 View 对象**/
LayoutInflater inflater = getLayoutInflater();
viewOne = inflater.inflate(R.layout.guide_one,null);
viewTwo = inflater.inflate(R.layout.guide_two,null);
viewThree = inflater.inflate(R.layout.guide_three,null);
viewArrayList = new ArrayList<>();
viewArrayList.add(viewOne); // 构建显示组件列表
viewArrayList.add(viewTwo);
viewArrayList.add(viewThree);
adapter = new MyPagerAdapter(viewArrayList);
viewPager.setAdapter(adapter); //添加适配器
button = viewThree.findViewById(R.id.btn_queding);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,GuideActivity.class);
startActivity(intent);
MainActivity.this.finish();
}
});
}
}
MyPagerAdapter.java
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<View> pageList=null; // 接收构造方法中传来的 view 列表
public MyPagerAdapter(ArrayList<View> pageList) {
this.pageList = pageList;
}
@Override
public int getCount() {
return pageList.size(); // 返回 view 列表的大小,即 view 的数目
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View pageView = pageList.get(position); // 获取当前位置的 view
container.addView(pageView); // 设置当前 view 为显示对象
return pageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(pageList.get(position)); // 移除当前位置的 view
}
}