ViewPager2 滑动图片浏览

news/2024/10/17 14:19:57/

1. 基于 "Gallery 简易图片浏览" 添加

Gallery 简易图片浏览https://blog.csdn.net/u011193452/article/details/127006679

2. ViewPager 适配器

  2.1 适配器布局文件 pager_photo_view.xml

<?xml version="1.0" encoding="utf-8"?>
<io.supercharge.shimmerlayout.ShimmerLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/shimmerLayout"android:layout_width="match_parent"android:layout_height="match_parent"><com.github.chrisbanes.photoview.PhotoViewandroid:id="@+id/pagerPhoto"android:layout_width="match_parent"android:layout_height="match_parent"android:adjustViewBounds="true" />
</io.supercharge.shimmerlayout.ShimmerLayout>

  2.2 创建 ViewPager 适配器 PagerPhotoListAdapter.kt

class PagerPhotoListAdapter : ListAdapter<PhotoItem, PagerPhotoViewHolder>(DiffCallback) {object DiffCallback : DiffUtil.ItemCallback<PhotoItem>() {override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {return oldItem == newItem}override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {return oldItem.photoId == newItem.photoId}}override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerPhotoViewHolder {return PagerPhotoViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.pager_photo_view, parent, false))}override fun onBindViewHolder(holder: PagerPhotoViewHolder, position: Int) {holder.view.shimmerLayout.apply {setShimmerColor(0x55FFFFFF)setShimmerAngle(0)startShimmerAnimation()}Glide.with(holder.itemView).load(getItem(position).fullUrl).placeholder(R.drawable.ic_photo_place_holder).listener(object : RequestListener<Drawable> {override fun onLoadFailed(e: GlideException?,model: Any?,target: Target<Drawable>?,isFirstResource: Boolean): Boolean {return false}override fun onResourceReady(resource: Drawable?,model: Any?,target: Target<Drawable>?,dataSource: DataSource?,isFirstResource: Boolean): Boolean {return false.also { holder.view.shimmerLayout.stopShimmerAnimation() }}}).into(holder.view.pagerPhoto)}
}class PagerPhotoViewHolder(itemView: View) : ViewHolder(itemView) {val view = PagerPhotoViewBinding.bind(itemView)
}

3. ViewPager 浏览图片页

  3.1 Fragment 导航布局, navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/navigation"app:startDestination="@id/galleryFragment"><fragmentandroid:id="@+id/galleryFragment"android:name="com.example.gallery.GalleryFragment"android:label="Gallery"tools:layout="@layout/fragment_gallery"><actionandroid:id="@+id/action_galleryFragment_to_pagerPhotoFragment"app:destination="@id/pagerPhotoFragment" /></fragment><fragmentandroid:id="@+id/pagerPhotoFragment"android:name="com.example.gallery.PagerPhotoFragment"android:label="PagerPhoto"tools:layout="@layout/fragment_pager_photo" />
</navigation>

  3.2 布局文件 frament_pager_photo.xml  

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:drawable/screen_background_dark"tools:context=".PagerPhotoFragment"><androidx.viewpager2.widget.ViewPager2android:id="@+id/viewPager2"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="50dp" /><TextViewandroid:id="@+id/photoTag"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="50dp"android:gravity="bottom|center_horizontal"android:text="@string/hello_blank_fragment"android:textColor="@color/white"android:textSize="20sp" />
</FrameLayout>

  3.3 传人值实现 GalleryAdapter.kt

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {val holder = MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.gallery_cell, parent, false))holder.itemView.setOnClickListener {Bundle().apply {//putParcelable("PHOTO", getItem(holder.adapterPosition))//holder.itemView.findNavController().navigate(R.id.action_galleryFragment_to_photoFragment,this)putParcelableArrayList("PHOTO_LIST", ArrayList(currentList))putInt("PHOTO_POSITION", holder.adapterPosition)holder.itemView.findNavController().navigate(R.id.action_galleryFragment_to_pagerPhotoFragment,this)}}return holder}

  3.4 实现ViewPager页 PagerPhotoFragment.kt

class PagerPhotoFragment : Fragment() {private lateinit var binding: FragmentPagerPhotoBindingoverride fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {binding = FragmentPagerPhotoBinding.inflate(inflater, container, false)return binding.root}override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)val photoList = arguments?.getParcelableArrayList<PhotoItem>("PHOTO_LIST")PagerPhotoListAdapter().apply {binding.viewPager2.adapter = thissubmitList(photoList)}binding.viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {override fun onPageSelected(position: Int) {super.onPageSelected(position)binding.photoTag.text = "${position + 1}/${photoList?.size}"}})binding.viewPager2.setCurrentItem(arguments?.getInt("PHOTO_POSITION") ?: 0, false)}
}

4. 效果图


http://www.ppmy.cn/news/150213.html

相关文章

简单图片浏览器

本示例用于循环显示GIF图片&#xff0c;调用Python的标准库tkinter&#xff0c;如果要做更高端的图像处理应该采用PIL库。 import tkinter as tk,os #导入库 class Application(tk.Frame): #定义GUI的应用程序类&#xff0c;派生于Frmaedef __init__(self,masterNone): #构…

Gallery 简易图片浏览

1. build.gradle, AndroidManifast.xml, 配置文件添加引用 1.1 dependencies 中添加引用库 dependencies {//http 请求implementation com.android.volley:volley:1.2.1//上拉刷新implementation androidx.swiperefreshlayout:swiperefreshlayout:1.1.0//加载网络地址图片impl…

照片浏览

照片浏览器&#xff1a; 1&#xff0c;SDPhotoBrowser 链接&#xff1a; https://github.com/gsdios/SDPhotoBrowser 介绍&#xff1a;中国人提供的源码&#xff0c;是在sdimage上进行开发的&#xff1b;可以查看网络图片&#xff0c;并且有图片缓存功能&#xff0c;加载一次后…

XnViewMP图片浏览器

今天小编给大家带来的是XnViewMP图片浏览器&#xff0c;这是一款非常棒的完全免费图片浏览器&#xff0c;同时支持多种语言&#xff0c;并且是纯绿色免安装&#xff0c;即下即用。软件支持100多种图片格式&#xff0c;还具有浏览器、幻灯片、屏幕捕捉、缩略图制作、批处理转换、…

页面图片浏览

在页面上浏览图片&#xff0c;实现图片的拖放 <html><head><meta http-equiv"Content-Type" content"text/html; charsetgb2312"><title>位图浏览器</title><style type"text/css"><!--#pic { width:…

Js图片浏览器

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns"http://www.w3.org/1999/xhtml"> <head> <title>Js图片浏览器</title&…

照片浏览器

昨天写了个图片浏览器&#xff0c;很简单&#xff0c;主要就是用一个JFileChooser导入一个文件&#xff0c;然后在文件父目录下导入下一个 或者上一个图像。 东西做好了&#xff0c;但是还有一个问题&#xff0c;就是我界面图片的路径设置有问题&#xff0c;在打成Jar包后就不能…

浏览器加载本地图片并预览

function imgPreview(config) {var width config.width || 200;var height config.height || 200;return {/*** ie9- 预览图片* param {type} input_file ,$(input[typefile]) 或者 $(input[typefile])[0]* param {type} callback 回掉参数为&#xff0c;$(居中的图片)* retu…