我正试图使android的全屏图像查看器片段。我使用ViewPager2。当片段开始时,第一个图像出现在顶部。当我向右滑动并回到第一张图片时,图片处于中心位置。为什么它第一次就排在第一位?
FullScreenImageFragment.kt
class FullScreenImageFragment : Fragment() {
private var images = arrayListOf<String>()
private lateinit var binding: FragmentFullScreenBinding
private lateinit var fullScreenImageSliderAdapter: FullScreenImageSliderAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
binding = FragmentFullScreenBinding.inflate(inflater)
if (arguments?.getStringArrayList(Constant.IMAGES) != null)
images = arguments?.getStringArrayList(Constant.IMAGES) as ArrayList<String>
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
fullScreenImageSliderAdapter = FullScreenImageSliderAdapter()
binding.fullScreenViewPager.adapter = fullScreenImageSliderAdapter
TabLayoutMediator(
binding.intoTabLayout,
binding.fullScreenViewPager
)
{ _, _ -> }.attach()
fullScreenImageSliderAdapter.setList(images)
}
}
FullScreenImageSliderAdapter.kt
class FullScreenImageSliderAdapter :
RecyclerView.Adapter<FullScreenImageSliderAdapter.ModelViewHolder>() {
private lateinit var itemBinding: ImageSliderItemBinding
private var images = arrayListOf<String>()
class ModelViewHolder(private val itemBinding: ImageSliderItemBinding) :
RecyclerView.ViewHolder(itemBinding.root) {
fun bindItems(s: String) {
Picasso.get().load(s).into(itemBinding.imageView)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ModelViewHolder {
val inflater = LayoutInflater.from(parent.context)
itemBinding = ImageSliderItemBinding.inflate(inflater, parent, false)
itemBinding.imageView.scaleType = ImageView.ScaleType.CENTER
return ModelViewHolder(itemBinding)
}
override fun getItemCount(): Int {
return images.size
}
override fun onBindViewHolder(holder: ModelViewHolder, position: Int) {
holder.bindItems(images[position])
}
fun setList(newImages: ArrayList<String>) {
images.clear()
images.addAll(newImages)
notifyDataSetChanged()
}
}
ImageItem.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/image_slider_item_background">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/placeholder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentFullScreen.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/fullScreenViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/into_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/transparent"
app:tabBackground="@drawable/tab_pager_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
</FrameLayout>
您可以在XML文件中尝试:
android:scaleType="center"