Android 《ViewPager 实现引导页》

布局文件

activity_launch_simple.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".LaunchSimpleActivity">
<androidx.viewpager.widget.ViewPager
    android:id="@+id/vg_launch"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

launch_item.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:ignore="MissingDefaultResource">
    <ImageView
        android:id="@+id/iv_launch_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        />

    <RadioGroup
        android:id="@+id/rg_launch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:orientation="horizontal"
        >
    </RadioGroup>

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="开始美好生活"
        android:textColor="#ff3300"
        android:textSize="22sp"
        android:visibility="gone"
        />
</RelativeLayout>

Activity

LaunchSimpleActivity.java

package com.galanz.app_ui_advance;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class LaunchSimpleActivity extends AppCompatActivity {
    //引导页图片数组
    private int[] launchImageArray = {
            R.drawable.guide_bg1,
            R.drawable.guide_bg2,
            R.drawable.guide_bg3,
            R.drawable.guide_bg4
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_simple);
        //隐藏actionBar
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.hide();

        ViewPager vg_launch = findViewById(R.id.vg_launch);

        vg_launch.setAdapter(new LauncherPageAdapter(this, launchImageArray));
    }
}

class LauncherPageAdapter extends PagerAdapter {

    private Context ctx;
    private int[] imageArray;
    private List<View> views = new ArrayList<>();


    public LauncherPageAdapter(Context ctx, int[] imageArray) {
        this.ctx = ctx;
        this.imageArray = imageArray;

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        for (int i = 0; i < imageArray.length; i++) {
            //构造View
            View view = LayoutInflater.from(ctx).inflate(R.layout.launch_item, null);
            ImageView iv_launch_img = view.findViewById(R.id.iv_launch_img);
            RadioGroup rg_launch = view.findViewById(R.id.rg_launch);
            Button btn_start = view.findViewById(R.id.btn_start);
            iv_launch_img.setImageResource(imageArray[i]);
            //分配一组radioButton
            for (int j = 0; j < imageArray.length; j++) {
                RadioButton btn = new RadioButton(ctx);
                btn.setLayoutParams(layoutParams);
                rg_launch.addView(btn);
                if (i == j) {
                    btn.setChecked(true);

                } else {
                    btn.setClickable(false);
                }
            }
            //是否显示按钮
            if (i == (imageArray.length - 1)) {
                btn_start.setVisibility(ViewGroup.VISIBLE);
                btn_start.setOnClickListener(v -> {
                    Intent intent = new Intent(ctx, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    LaunchSimpleActivity act = (LaunchSimpleActivity) ctx;
                    act.startActivity(intent);
                });
            } else {
                btn_start.setVisibility(ViewGroup.GONE);
            }
            views.add(view);
        }

    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = views.get(position);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(views.get(position));
    }
}

实现效果

image

posted @ 2024-02-20 16:15  一个小笨蛋  阅读(32)  评论(0编辑  收藏  举报