2

安卓工作室 3.6

在我的活动中:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding =
            DataBindingUtil.setContentView<SelectionActivityBinding>(
                this,
                R.layout.selection_activity
            )
        dataBinding.setHandler(this)

        setSupportActionBar(dataBinding.toolBarContainer.toolBar)
        getSupportActionBar()!!.setDisplayHomeAsUpEnabled(true)
        getSupportActionBar()!!.setDisplayShowTitleEnabled(false)

        val articlesList = mutableListOf<Article>()
        val article1 = Article()
        article1.title = "111111"
        articlesList.add(article1)

        val article2 = Article()
        article1.title = "222222"
        articlesList.add(article2)

        articleAdapter = ArticleAdapter()
        articleAdapter.setList(articlesList)
        dataBinding.viewPager2.adapter = articleAdapter
    }

这里的xml布局:

<?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>

        <variable
            name="handler"
            type="com.myproject.ui.activity.SelectionActivity" />

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/waitressCallMainContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/toolBarContainer"
            layout="@layout/tool_bar"
            android:title='@{@string/articles}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="0dp"
            android:layout_height="300dp"
            android:background="#bbccaa"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

这里自定义适配器:

class ArticleAdapter : RecyclerView.Adapter<ArticleAdapter.ArticleViewHolder>() {
    var articleList: List<Article> = mutableListOf()

    companion object {
        private val TAG = ArticleAdapter::class.java.name
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
        return ArticleViewHolder(parent)
    }

    override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
        Debug.d(TAG, "onBindViewHolder: position = $position")
        holder.bind(articleList[position])
    }

    fun setList(articleList: List<Article>) {
        this.articleList = articleList
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = articleList.size

    class ArticleViewHolder constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
        constructor(parent: ViewGroup) :
                this(
                    LayoutInflater.from(parent.context).inflate(
                        R.layout.article_item,
                        parent,
                        false
                    )
                )

        fun bind(article: Article) {
            Debug.d(TAG, "bind: article = $article")
            itemView.titleTextView.text = article.title
        }
    }
}

但结果它显示空的viewpager2(绿色背景)。viewPager2 中没有内部项目。结果没有刷卡。

在此处输入图像描述

4

0 回答 0