使用ViewPager实现页面滑动
GuideActivity
public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener{ private ViewPager vpSlide;//滑动界面 private int [] imageArray;//图片数组 private List<View> viewList;//图片集合 private Button btnInto;//进入按钮 @Override protected int getView() { return R.layout.activity_guide; } @Override protected void initView() { //隐藏状态栏和导航栏 Common.hideBar(this); btnInto = findViewById(R.id.btn_into); vpSlide = findViewById(R.id.vp_slide); //实例化图片资源 imageArray = new int[]{R.drawable.icon_1,R.drawable.icon_2,R.drawable.icon_3}; viewList= new ArrayList<View>(); } @Override protected void method() { initViewPager(); setClick(); } /** * 加载图片ViewPager */ private void initViewPager() { //循环创建View并加入到集合中 int len = imageArray.length; for (int i = 0;i<len;i++){ //将ImageView加入到集合中 ImageView imageView = new ImageView(this); imageView.setBackgroundResource(imageArray[i]); viewList.add(imageView); } //View集合初始化好后,设置Adapter vpSlide.setAdapter(new GuideAdapter(viewList)); //设置滑动监听 vpSlide.setOnPageChangeListener(this); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } /** * 滑动后的监听 * 判断是否是最后一页,若是则显示按钮 */ @Override public void onPageSelected(int position) { if (position == imageArray.length - 1){ btnInto.setVisibility(View.VISIBLE); }else { btnInto.setVisibility(View.GONE); } } @Override public void onPageScrollStateChanged(int state) { } private void setClick() { btnInto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(GuideActivity.this,MainActivity.class)); GuideActivity.this.finish(); } }); } }
activity_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"> <androidx.viewpager.widget.ViewPager android:id="@+id/vp_slide" android:layout_width="match_parent" android:layout_height="match_parent"/> <Button android:id="@+id/btn_into" android:layout_width="80dp" android:layout_height="45dp" android:text="进入" android:visibility="gone" android:textColor="#FFF" android:textSize="18sp" android:background="@drawable/splash_btn_shape" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/> </RelativeLayout>