安卓基础之《(21)—高级控件(3)翻页类视图》

一、翻页视图ViewPager

1、翻页视图允许页面在水平方向左右滑动

2、例子

这里用更新的类ViewPager2

ViewPagerActivity.java

java 复制代码
package com.example.chapter08;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.example.chapter08.adapter.ImagePagerAdapter;

public class ViewPagerActivity extends AppCompatActivity {

    public static final int[] imageList = new int[] {R.drawable.diqiu, R.drawable.shuixing, R.drawable.huoxing};
    public static final String[] descList = new String[] {"地球", "水星", "火星"};

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

        ViewPager2 vp2_content = findViewById(R.id.vp2_content);
        ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageList, descList);
        vp2_content.setAdapter(adapter);
        vp2_content.setCurrentItem(1, false); // 设置初始位置
        // 设置二代翻页视图的排列方向为水平方向
        vp2_content.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        vp2_content.setCurrentItem(0);
    }
}

布局文件activity_view_pager.xml

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=".ViewPagerActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp2_content"
        android:layout_width="match_parent"
        android:layout_height="370dp"/>

</LinearLayout>

适配器ImagePagerAdapter.java

java 复制代码
package com.example.chapter08.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chapter08.R;

public class ImagePagerAdapter extends RecyclerView.Adapter{

    private Context mContext;
    private int[] mImageList;
    private String[] mDescList;

    // 定义列表项的视图持有者
    class ItemHolder extends RecyclerView.ViewHolder {
        public ImageView iv_pic; // 声明列表项图标的图像视图
        public TextView tv_desc; // 声明列表项描述的文本视图

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            iv_pic = itemView.findViewById(R.id.iv_pic);
            tv_desc = itemView.findViewById(R.id.tv_desc);
        }
    }

    public ImagePagerAdapter(Context context, int[] imageList, String[] descList) {
        this.mContext = context;
        this.mImageList = imageList;
        this.mDescList = descList;
    }

    // 创建视图持有者(ViewHolder),并实例化对应的布局文件
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_pager2, viewGroup, false);
        return new ItemHolder(itemView);
    }

    // 将数据绑定到指定位置的视图上
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemHolder ih = (ItemHolder) holder;
        ih.iv_pic.setImageResource(mImageList[position]);
        ih.iv_pic.setLayoutParams(new LinearLayout.LayoutParams(
                // 设置长宽
                800, 800));
        ih.tv_desc.setText(mDescList[position]);
    }

    // 返回列表项数量
    @Override
    public int getItemCount() {
        return mImageList.length;
    }
}

条目文件item_pager2.xml

XML 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:scaleType="fitCenter" />
    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
相关推荐
HLC++12 小时前
Linux的进程间通信
android·linux·服务器
爱笑鱼16 小时前
Binder(二):AIDL 生成的 Proxy、Stub 和 Parcel 到底在做什么?
android
爱笑鱼16 小时前
Binder(一):一次方法调用,究竟怎样跨进程执行?
android
壮哥_icon17 小时前
【Android 系统开发】使用 BAT 脚本高效自动化管理 /system/priv-app/ 系统应用(安装与卸载)
android·运维·自动化
用户693717500138418 小时前
Claude Code终端日志Token占用实测:一个过滤器砍掉60%-90%
android·前端·后端
蝉蜕日记19 小时前
AccumuPDF高级版 v2.57 | 视图文转换PDF,合并分割压缩
android·智能手机·pdf·生活·软件需求
吐了啊取名字太难19 小时前
美颜系统AI修图本地跑并支持Mac、win、安卓、iOS不卡顿
android·人工智能·windows·数码相机·mac·ai编程
阿pin20 小时前
Android随笔-Retrofit
android·retrofit
想取一个与众不同的名字好难20 小时前
安卓自定义颜色选择器
android
我命由我1234521 小时前
Android 开发问题:java.lang.NoSuchMethodError:No virtual method readAllBytes()
android·java·java-ee·kotlin·android studio·android-studio·android runtime