IllegalStateException:在 ViewPager2 具有适配器之前附加 TabLayoutMediator
IllegalStateException: TabLayoutMediator attached before ViewPager2 has an adapter
fragment_news_details
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data class="NewsDetailsBinding">
<variable
name="adapter"
type="com.abc.xyz.base.BaseAdapter" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout 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=".news.NewsDetailsFragment">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/viewPagerGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/imageViewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:adapter="@{adapter}"
app:layout_constraintBottom_toBottomOf="@id/viewPagerGuideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/dotsTab"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@color/transparent"
app:layout_constraintBottom_toBottomOf="@id/imageViewPager"
app:layout_constraintEnd_toEndOf="@+id/imageViewPager"
app:layout_constraintStart_toStartOf="@+id/imageViewPager"
app:tabBackground="@drawable/dot_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
<TextView
android:id="@+id/descriptionTitleTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="@color/textColorPrimary"
android:textSize="@dimen/text_size_20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/viewPagerGuideline"
tools:text="Fresh snow at Dunga" />
<TextView
android:id="@+id/descriptionTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="@color/textColorSecondary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/descriptionTitleTV"
tools:text="Lorem ipsum dolor sit." />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout><?xml version="1.0" encoding="utf-8"?>
NewsDetailsFragment
class NewsDetailsFragment : Fragment() {
lateinit var binding: NewsDetailsBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = inflater.bind(R.layout.fragment_news_details, container)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val templist = ArrayList<String>().apply {
add("https://picsum.photos/id/237/200/300")
add("https://picsum.photos/seed/picsum/200/300")
add("https://picsum.photos/id/1002/300/400")
add("https://picsum.photos/id/237/200/300")
}
binding.adapter =
BaseAdapter(templist, R.layout.item_news_image, ::NewsImageViewHolder)
if (binding.adapter != null)
TabLayoutMediator(binding.dotsTab, binding.imageViewPager) { tab, position ->
tab.icon = ContextCompat.getDrawable(requireContext(), R.drawable.anim_add_star)
}.attach()
}
inner class NewsImageViewHolder(itemBinding: NewsImageItemBinding) :
BaseViewHolder<String, NewsImageItemBinding>(itemBinding) {
}
}
在 activity 上创建了它的抛出错误 TabLayoutMediator 在 ViewPager2 具有适配器之前附加。我尝试在 TabloutMediatorI().attach() 的上方和下方添加适配器,但在这两种情况下都不起作用。我需要的是使用 tabslayout 显示点指示器。我是 ViewPager 的新手2 ,任何帮助都是 appreciated.Thanks.
ViewPager 没有适配器属性,因此 android:adapter="@{adapter}"
不会在 ViewPager 上设置适配器。
相反,您可以从绑定中获取视图寻呼机,并像这样在您的片段中设置适配器:binding.imageViewPager.adapter = BaseAdapter(templist, R.layout.item_news_image, ::NewsImageViewHolder)
fragment_news_details
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data class="NewsDetailsBinding">
<variable
name="adapter"
type="com.abc.xyz.base.BaseAdapter" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout 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=".news.NewsDetailsFragment">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/viewPagerGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/imageViewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:adapter="@{adapter}"
app:layout_constraintBottom_toBottomOf="@id/viewPagerGuideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/dotsTab"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@color/transparent"
app:layout_constraintBottom_toBottomOf="@id/imageViewPager"
app:layout_constraintEnd_toEndOf="@+id/imageViewPager"
app:layout_constraintStart_toStartOf="@+id/imageViewPager"
app:tabBackground="@drawable/dot_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
<TextView
android:id="@+id/descriptionTitleTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="@color/textColorPrimary"
android:textSize="@dimen/text_size_20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/viewPagerGuideline"
tools:text="Fresh snow at Dunga" />
<TextView
android:id="@+id/descriptionTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="@color/textColorSecondary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/descriptionTitleTV"
tools:text="Lorem ipsum dolor sit." />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout><?xml version="1.0" encoding="utf-8"?>
NewsDetailsFragment
class NewsDetailsFragment : Fragment() {
lateinit var binding: NewsDetailsBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = inflater.bind(R.layout.fragment_news_details, container)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val templist = ArrayList<String>().apply {
add("https://picsum.photos/id/237/200/300")
add("https://picsum.photos/seed/picsum/200/300")
add("https://picsum.photos/id/1002/300/400")
add("https://picsum.photos/id/237/200/300")
}
binding.adapter =
BaseAdapter(templist, R.layout.item_news_image, ::NewsImageViewHolder)
if (binding.adapter != null)
TabLayoutMediator(binding.dotsTab, binding.imageViewPager) { tab, position ->
tab.icon = ContextCompat.getDrawable(requireContext(), R.drawable.anim_add_star)
}.attach()
}
inner class NewsImageViewHolder(itemBinding: NewsImageItemBinding) :
BaseViewHolder<String, NewsImageItemBinding>(itemBinding) {
}
}
在 activity 上创建了它的抛出错误 TabLayoutMediator 在 ViewPager2 具有适配器之前附加。我尝试在 TabloutMediatorI().attach() 的上方和下方添加适配器,但在这两种情况下都不起作用。我需要的是使用 tabslayout 显示点指示器。我是 ViewPager 的新手2 ,任何帮助都是 appreciated.Thanks.
ViewPager 没有适配器属性,因此 android:adapter="@{adapter}"
不会在 ViewPager 上设置适配器。
相反,您可以从绑定中获取视图寻呼机,并像这样在您的片段中设置适配器:binding.imageViewPager.adapter = BaseAdapter(templist, R.layout.item_news_image, ::NewsImageViewHolder)