我正在尝试用viewpager2创建全屏图像滑块。但当我运行代码时,第一页不会显示任何图像,但当我滑动到另一页并重新访问第一页时,图像就会显示。我尝试了所有的方法,但都没有找到问题。请帮忙。这是我的密码。
PS-我把图像改成了彩色,这样可以很容易地看到屏幕上的布局。即使我将图像列表而不是颜色列表传递给适配器,也会发生同样的事情。
我的适配器:
class FullScreenImageAdapter(val context: Context, val sliderItems: MutableList<Int>, val viewPager2: ViewPager2) : RecyclerView.Adapter<FullScreenImageAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.fullscreen_image_container,
parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.imageView.setBackgroundResource(sliderItems[position])
}
override fun getItemCount(): Int {
return sliderItems.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView= itemView.findViewById(R.id.fullImageId)
}
}
fullscreen_image_container.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fullImageId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop">
</ImageView>
我的片段中的代码:
class FullScreenImageFragment : Fragment() {
lateinit var viewPager2: ViewPager2
lateinit var navController: NavController
lateinit var sliderAdapter: FullScreenImageAdapter
var sliderImages: MutableList<Int> = mutableListOf()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_full_screen_image, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = view.findNavController()
sliderImages.add(R.color.Green)
sliderImages.add(R.color.Orange)
sliderImages.add(R.color.black)
sliderImages.add(R.color.colorchery)
header_layout_fullscreen.app_name_header.visibility = View.GONE
header_layout_fullscreen.image_back_btn.setOnClickListener {
navController.navigateUp()
}
viewPager2 = view.findViewById(R.id.full_image_viewpager) as ViewPager2
sliderAdapter = FullScreenImageAdapter(requireContext(), sliderImages, viewPager2)
viewPager2.adapter = sliderAdapter
}
}
fragment_full_screen_image.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.FullScreenImageFragment">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/full_image_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
感谢
编辑:-我创建了一个新的活动,并将我的所有代码从片段转移到了活动,它在那里运行得非常好。我仍然不知道碎片有什么问题。如果你们中的任何人都能找到碎片,我仍然想知道它的问题。我更喜欢在应用程序中使用单个活动,但我现在会使用这个解决方法。
为第一个屏幕提供默认数据,否则由于某种原因,它会显示为空/null。例如:
data class SomeDataClass(
val title: String = "Title1",
val content: String = "Content1"): Serializable