Android:ViewPager滑动切换View

用ViewPager滑动切换View的方式做一个常见的软件欢迎页面

1.创建一个Activity:Guide.java作为第一次使用软件的导航页面

Cuide.java

public class Guide extends Activity implements ViewPager.OnPageChangeListener{
    private ViewPager vp;
    private ViewPagerAdapter adapter;
    private List<View> views;
    private ArrayList<ImageView> ivs;
    private int[] ids = {R.id.iv1,R.id.iv2,R.id.iv3};
    private Button btn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_layout);
        initView();
        initIv();
    }

    private void initView() {
        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));
        adapter = new ViewPagerAdapter(views,this);
        vp = findViewById(R.id.viewpager);
        vp.setAdapter(adapter);
        vp.setOnPageChangeListener(this);
        btn = (Button) views.get(2).findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Guide.this,MainActivity.class));
                finish();
            }
        });
    }

    private void initIv() {
        ivs = new ArrayList<>();
        for (int i = 0;i<ids.length;i++) {
            ivs.add((ImageView) findViewById(ids[i]));
        }
    }

    //当页面被滑动时调用
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
    //当前新的页面被选中时调用
    @Override
    public void onPageSelected(int position) {
        System.out.println(position);
        for (int i=0;i<ids.length;i++) {
            if (i == position) {
                ivs.get(i).setBackgroundResource(R.color.green);
            }else {
                ivs.get(i).setBackgroundResource(R.color.white);
            }
        }
    }
    //当滑动状态改变时调用
    @Override
    public void onPageScrollStateChanged(int state) {

    }
}

布局文件guide_layout.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.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000">
    </android.support.v4.view.ViewPager>
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true">
        <ImageView
            android:id="@+id/iv1"
            android:background="#00ff00"
            android:src="@drawable/nazi3"
            android:layout_width="100dp"
            android:layout_height="50dp" />
        <ImageView
            android:id="@+id/iv2"
            android:background="#ffffff"
            android:src="@drawable/nazi4"
            android:layout_width="100dp"
            android:layout_height="50dp" />
        <ImageView
            android:id="@+id/iv3"
            android:background="#ffffff"
            android:src="@drawable/lol"
            android:layout_width="100dp"
            android:layout_height="50dp" />
    </LinearLayout>
</RelativeLayout>
2.设置三个被切换的View,加载不同的图片

one.xml

<?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_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/nazi3"/>
</LinearLayout>

two.xml

<?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_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/nazi4"/>
</LinearLayout>

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">
    <ImageView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/lol"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:alpha="250"
        android:text="进入" />
</RelativeLayout>

在这里插入图片描述

3.欢迎页面

WelcomeActivity.java,使用SharePreferences存储状态值,判断是不是第一次打开软件.如果是第一次使用,跳转到导航页面,如果不是,直接跳转到主界面

public class WelcomeActivity extends AppCompatActivity {

    private static final int TIME = 2000;
    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 10001;
    private boolean is = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        init();
    }

    private void init() {
        //使用SharePreferences存储状态值,判断是不是第一次打开软件
        SharedPreferences sp = getSharedPreferences("zs",MODE_PRIVATE);
        is = sp.getBoolean("zs",true);
        //如果是第一次使用,跳转到导航页面,如果不是,直接跳转到主界面
        if (!is) {
            handler.sendEmptyMessageDelayed(GO_HOME,TIME);
        }else {
            handler.sendEmptyMessageDelayed(GO_GUIDE,TIME);
            SharedPreferences.Editor ed = sp.edit();
            ed.putBoolean("zs",false);
            ed.commit();
        }
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    goHome();
                    break;
                case GO_GUIDE:
                    goGuide();
                    break;
            }
        }
    };

    private void goHome() {
        startActivity(new Intent(this,MainActivity.class));
        finish();
    }

    private void goGuide() {
        startActivity(new Intent(this,Guide.class));
        finish();
    }
}

activity_welcome.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="zs.viewpagerdemo.WelcomeActivity">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/lol"/>
</android.support.constraint.ConstraintLayout>
4.主界面

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.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="zs.viewpagerdemo.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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你这个橘子不要皮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值