ViewPager or RecyclerviewAndroid开发Android控件使用篇

ViewPager2实现仿抖音竖屏翻页效果

2021-04-25  本文已影响0人  千夜零一

ViewPager和ViewPager2的区别

实现关键代码:

vBinding.viewPager2.adapter = ViewPagerAdapter()
vBinding.viewPager2.orientation = ViewPager2.ORIENTATION_VERTICAL

国际惯例:先看效果



功能实现

1、使用viewBinding绑定布局id

val vBinding = ActivityKotlin11Binding.inflate(layoutInflater)
setContentView(vBinding.root)

2、布局代码

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".display.Kotlin11">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

3、Activity代码

/**
 * viewPager2竖屏切换
 */
@Route(path = RouterPath.kotlin11)
class Kotlin11 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val vBinding = ActivityKotlin11Binding.inflate(layoutInflater)
        setContentView(vBinding.root)
        vBinding.viewPager2.adapter = ViewPagerAdapter()
        vBinding.viewPager2.orientation = ViewPager2.ORIENTATION_VERTICAL
    }
}

4、ViewPagerAdapter代码

/**
 * @data on 4/25/21 11:28 AM
 * @auther KC
 * @describe viewPager适配器
 */
class ViewPagerAdapter : RecyclerView.Adapter<ViewPagerAdapter.MyViewHolder>() {

    val colorList = ArrayList<Int>()

    init {
        colorList.add(R.color.blue)
        colorList.add(R.color.red)
        colorList.add(R.color.green)
        colorList.add(R.color.pink)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.viewpager_item, parent, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.mTvTitle?.text = "item " + position;
        holder.itemView.setBackgroundResource(colorList[position]);
    }

    override fun getItemCount(): Int = colorList.size

    class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        var mTvTitle: TextView? = itemView.findViewById(R.id.tvTitle)
    }
}

5、viewPager子布局代码:R.layout.viewpager_item.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"
    android:id="@+id/container"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/tvTitle"
        tools:text="item"
        android:layout_centerInParent="true"
        android:textColor="@android:color/white"
        android:textSize="32dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

大功告成!

上一篇下一篇

猜你喜欢

热点阅读