如何在Kotlin中实现RecyclerViewPager?

时间:2019-10-21 01:35:37

标签: android kotlin androidx

我正在尝试在项目中实现RecyclerViewPagerIndicator,但是无法正常工作。

我使用Kotlin和AndroidX

我希望从数据库发送一个包含文本和图像的数组,并显示在RecyclerViewPager中,就像火种。

1 个答案:

答案 0 :(得分:0)

我建议您使用dotsIndicator,因为它

  • 支持基于ViewPager 2的{​​{1}}
  • 得到维护,您选择的不是(项目似乎已放弃)
  • RecyclerView
  • 编写

示例:

activity_main.xml

Kotlin

MainActivity.kt

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager_2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <com.tbuonomo.viewpagerdotsindicator.WormDotsIndicator
        android:id="@+id/dots_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"/>

</LinearLayout>

PagerAdapter.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setupViewPager2()
    }

    private fun setupViewPager2() {
        val viewPager2 = findViewById<ViewPager2>(R.id.view_pager_2)
        val dotsIndicator = findViewById<WormDotsIndicator>(R.id.dots_indicator)

        viewPager2.adapter = PagerAdapter(this)
        dotsIndicator.setViewPager2(viewPager2)
    }
}

fragment_sample.xml

class PagerAdapter(fa:FragmentActivity): FragmentStateAdapter(fa) {

    override fun getItemCount(): Int = 3

    override fun createFragment(position: Int): Fragment {
        return when(position){
            0 -> SampleFragment.newInstance("One", "desc 1")
            1 -> SampleFragment.newInstance("Two", "desc 2")
            2 -> SampleFragment.newInstance("Three", "desc 3")
            else -> throw Exception("No such position")
        }
    }
}

SampleFragment.kt

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    tools:context=".SampleFragment">
    <TextView
        android:id="@+id/txt_header"
        tools:text="Header 1"
        android:textAlignment="center"
        android:textSize="24sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/txt_desc"
        tools:text="Desc 1"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>