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)); } }
实现效果
本文来自博客园,作者:一个小笨蛋,转载请注明原文链接:https://www.cnblogs.com/paylove/p/18023373
分类:
Android 代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效