ViewPager控件之PagerAdapter适配器

article/2025/9/23 1:05:03

一、ViewPager的基本用法

  1、简介

        ViewPager可以实现多个界面的左右滑动。ViewPager最典型的应用场景主要包 括引导页导航,轮转广告和页面菜单。

        ViewPager最早出自4.0版本,为了兼容低版本安卓设备,谷歌官方给我们提供了 一个的软件包android.support.v4.view。

  2、ViewPager的基本用法

1、在主界面中放置 ViewPager控件

2、创建用于滑动切 换的视图

3、将创建好的多个 视图装入View数组

4、定义PagerAdapter适配 器管理要显示的页

5、绑定适配器和 ViewPager控件

二、PagerAdapter适配器

1、PagerAdapter就是用于“将多个页面填充到ViewPager”的一个基类适配器。

2、在实际使用中,往往会自定义一个具体的适配器,并让它继承PagerAdapter。

3、继承PagerAdapter基类之后,往往需要在程序中重写相应的方法。

 三、代码部分

  1、activity_main.xml

   添加ViewPager控件

<?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"tools:context=".MainActivity"><android.support.v4.view.ViewPagerandroid:id="@+id/viewpager"android:layout_width="match_parent"android:layout_height="wrap_content"></android.support.v4.view.ViewPager>
</LinearLayout>

  2、page1.xml;page2.xml;page3.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"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/view1"/>
</RelativeLayout>
<?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"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/view2"/>
</RelativeLayout>
<?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"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/view3"/>
</RelativeLayout>

  3、MainActivity.java

package com.example.zsviewpager;import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {//1.创建View对象,以及View数组private View view1,view2,view3;private List<View> viewList;private MyPagerAdapter myPagerAdapter;private ViewPager viewPager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//2.将前面定义的滑动布局转换成view对象LayoutInflater  inflater = getLayoutInflater();view1 = inflater.inflate(R.layout.page1,null);view2 = inflater.inflate(R.layout.page2,null);view3 = inflater.inflate(R.layout.page3,null);//3.将前面定义的view对象存入view数组viewList = new ArrayList<View>();viewList.add(view1);viewList.add(view2);viewList.add(view3);//4.定义自定义适配器myPagerAdapter = new MyPagerAdapter(viewList);//5.将适配器和ViewPager控件进行绑定viewPager = findViewById(R.id.viewpager);viewPager.setAdapter(myPagerAdapter);}
}

  4、MyPagerAdapter.java

     这是继承于PagerAdapter的

package com.example.zsviewpager;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class MyPagerAdapter extends PagerAdapter {//1.定义view数组,用来接收从Activity传递过来的view数组private List<View> pageList;//2.定义MyPagerAdapter类的构造方法,接收数据public MyPagerAdapter(List<View> viewList){this.pageList = viewList;}@Overridepublic int getCount() {return pageList.size();}@Overridepublic boolean isViewFromObject(@NonNull View view, @NonNull Object o) {return view == o;}@Overridepublic void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {//将当前位置的view视图删除container.removeView(pageList.get(position));}@NonNull@Overridepublic Object instantiateItem(@NonNull ViewGroup container, int position) {//将当前视图添加到container中container.addView(pageList.get(position));//返回当前的view视图return  pageList.get(position);}
}

到此我们就是实现了图片滑动的效果。


http://chatgpt.dhexx.cn/article/HngOHqAc.shtml

相关文章

ViewPager onPageChangeListener总结

android ViewPager滑动事件讲解 今天在做项目的时候&#xff0c;由于要处理viewPager页面滑动的事件&#xff0c;所以对其进行了一个小小的研究&#xff1a; 首先ViewPager在处理滑动事件的时候要用到OnPageChangeListener OnPageChangeListener这个接口需要实现三个方法&am…

ViewPager onPageChangeListener总结

android ViewPager滑动事件讲解 今天在做项目的时候&#xff0c;由于要处理viewPager页面滑动的事件&#xff0c;所以对其进行了一个小小的研究&#xff1a; 首先ViewPager在处理滑动事件的时候要用到OnPageChangeListener OnPageChangeListener这个接口需要实现三个方法&#…

iscsi命令

iscsi客户端命令 iscsiadm 1.发现target设备 iscsiadm -m discovery -t sendtargets -p 10.12.22.61 疑问&#xff1a;10.12.22.60&#xff1a;3260&#xff0c;1 1表示什么意思(表示portal group tag,参考补充资料1)&#xff0c;此外&#xff0c;为什么连同ip为60的信息…

iSCSI 协议

iSCSI 协议 iSCSI协议结构 如同任何一个协议一样&#xff0c;iSCSI也有一个清晰的层次结构&#xff0c;根据OSI模型&#xff0c;iSCSI的协议栈自顶向下一共可以分为五层&#xff0c;如图所示&#xff1a; SCSI层&#xff1a;根据应用发出的请求建立SCSI CDB(命令描述块)&…

Linux与ISCSI

ISCSI&#xff08;Internet Small Computer System Interface&#xff09;:Internet 小型计算机系统接口&#xff0c;是一个基于 TCP/IP 的协 议&#xff0c;主要用于通过 IP 网络仿真 SCSI&#xff0c;从而为远程块存储设备提供数据传输和管理。说白了&#xff0c;就是通过 网…

ISCSI服务

Internet Small Computer System Interface:Internet 小型计算机系统接口&#xff0c;是一个基于 TCP/IP 的协 议&#xff0c;主要用于通过 IP 网络仿真 SCSI&#xff0c;从而为远程块存储设备提供数据传输和管理。说白了&#xff0c;就是通过 网络由专门的服务器提供存储管理&…

iSCSI 网络磁盘共享

17.1 iSCSI技术介绍 iSCSI技术实现了物理硬盘设备与TCP/TP网络传输协议的相互结合&#xff0c;使得用户可以通过互联网方便的获取到远程机房提供的共享存储资源 硬盘是计算机硬件设备中重要的组成部分之一&#xff0c;存储设备的IO读写速度快慢也直接影响着服务器整体性能的高…

ISCSI的部署与安装

iSCSI&#xff08;Internet Small Computer System Interface&#xff0c;Internet小型计算机系统接口&#xff09;是一种由IBM公司研究开发的IP SAN技术。 该技术是将现有SCSI接口与以太网络(Ethernet)技术结合&#xff0c;基于 TCP/IP的协议连接iSCSI服务端&#xff08;Targe…

SCSI协议与iSCSI协议

文章目录 1.SCSI协议2.iSCSI协议3.IO--SCSI错误处理机制 1.SCSI协议 SCSI的起源 SCSI出现的原因主要是因为原来的IDE接口的硬盘转速太慢&#xff0c;传输速率太低&#xff0c;因此高速的SCSI硬盘出现。其实SCSI并不是专为硬盘设计的&#xff0c;实际上它是一种总线型接口。由…

ISCSI详解(三)——ISCSI原理和架构

今天继续给大家介绍Linux运维相关知识&#xff0c;本文主要内容是ISCSI基础知识。 一、ISCSI架构 ISCSI的架构主要有以下两种&#xff1a; 1、控制器架构 在控制器架构下&#xff0c;ISCSI使用专门的数据传输芯片&#xff0c;专门的RAID数据校验芯片、高性能的cache缓存以及专…

iscsi服务器搭建

iscsi搭建 ISCSI服务介绍服务器配置&#xff08;IP:192.168.155.28&#xff09;客户端配置Linux&#xff08;IP:192.168.155.30&#xff09;Windows配置&#xff08;192.168.155.200&#xff09; ISCSI服务介绍 全称&#xff1a;Internet Small Computer System Interface——…

Windows iSCSI

iSCSI 题目一、安装iSCSI并创建存储位置二、配置iSCSI三、DC1连接iSCSI四、创建盘提示:若需要问题欢迎私聊‘ 题目 iSCSI 磁盘存储在D:\ISCSIDATA 中; iSCSI 磁盘提供给DC1 使用,磁盘容量500 G,启用chap 验证; DC1 上连接成功后,把磁盘格式化为NTFS 格式并挂载到卷标D …

【基于CentOS 7 的iscsi服务】

目录 一、概述 1.简述 2.作用 3. iscsi 4.相关名称 二、使用步骤 - 构建iscsi服务 1.使用targetcli工具进入到iscsi服务器端管理界面 2.实现步骤 2.1 服务器端 2.2 客户端 2.2.1 安装软件 2.2.2 在认证文件中生成iqn编号 2.2.3 开启客户端服务 2.2.4 查找可用的i…

ISCSI服务器搭建与配置

ISCSI服务简介 ISCSI简介&#xff1a; iSCSI( Internet Small Computer System Interface 互联网小型计算机系统接口) 技术是一种新存储技术&#xff0c;该技术是将现有的SCSI接口与以太网技术相结合&#xff0c;使服务器可与使用IP网络的存储装置互相交换资料。 iscsi 结构…

SCSI/ISCSI协议

SCSI即小型计算机接口&#xff08;Small Computer System Interface&#xff09;&#xff0c;指的是一个庞大协议体系&#xff0c;到目前为止经历了SCSI-1/SCSI-2/SCSI-3变迁。 SCSI协议定义了一套不同设备&#xff08;磁盘&#xff0c;磁带&#xff0c;处理器&#xff0c;光设…

iSCSI协议简介

本文综合了几篇参考文献的内容&#xff0c;做了删减与重组&#xff0c;但严格来说&#xff0c;不算原创。 笔者笔记如下&#xff1a; iSCSI initiator和target的核心功能都在内核中&#xff0c;无须人工干预&#xff1b;而要人工干预的大约是这么几件事&#xff1a; 1. 建立…

【存储】SCSI、iSCSI协议详解及对比

作 者&#xff1a; NGDCN 原文链接&#xff1a;【存储】SCSI、iSCSI协议详解及对比 - NGDCN 版 权&#xff1a; 本文由 NGDCN 于2022-11-11原创发布在 NGDCN&#xff0c;未经许可&#xff0c;禁止转载。 SCSI SCSI是小型计算机系统接口&#xff08;Small Compu…

ISCSI详解(二)——ISCSI基础知识

今天继续给大家介绍Linux运维相关知识&#xff0c;本文主要内容是ISCSI基础知识。 一、ISCSI简介 ISCSI&#xff0c;即Internel Small Computer System Interface&#xff0c;互联网小型计算机接口&#xff0c;就是在互联网上运行SCSI协议的一种技术。 ISCSI把原本用于用于计…

ISCSI介绍

ISCSI Internet Small Computer System Interface ISCSI是一种在Internet协议上&#xff0c;特别是以太网上进行数据块传输的标准&#xff0c;它是一种基于IP Storage理论的新型存储技术&#xff0c;该技术是将存储行业广泛应用的SCSI接口技术与IP网络技术相结合&#xff0c;…

iSCSI详解及 iSCSI服务端配置、客户端(Linux、Windows配置)

一、简介&#xff1a; iSCSI&#xff0c;Internet小型计算机系统接口&#xff0c;又称为IP-SAN&#xff0c;是一种基于因特网及SCSI-3协议下的存储技术&#xff0c;由IETF提出&#xff0c;并于2003年2月11日成为正式的标准。iscsi通过TCP/IP协议&#xff0c;实现远程存储单元的…