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.PhotoView
android: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">
<fragment
android:id="@+id/galleryFragment"
android:name="com.example.gallery.GalleryFragment"
android:label="Gallery"
tools:layout="@layout/fragment_gallery">
<action
android:id="@+id/action_galleryFragment_to_pagerPhotoFragment"
app:destination="@id/pagerPhotoFragment" />
</fragment>
<fragment
android: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.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp" />
<TextView
android: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: FragmentPagerPhotoBinding
override 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 = this
submitList(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. 效果图
原文地址:https://blog.csdn.net/u011193452/article/details/127049762
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_30140.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。