我试图在 recyclerview 中使用环氧树脂显示一个简单的文本视图,但它没有出现。我可能做错了什么。
我期待这样的事情
但我得到了这个
//build.gradle
implementation "com.airbnb.android:epoxy:4.3.1"
kapt "com.airbnb.android:epoxy-processor:4.3.1"
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rv= findViewById<RecyclerView>(R.id.main_recyclerview)
rv.adapter= MyController().adapter
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的控制器.kt
class MyController : Typed2EpoxyController<List<String>,Boolean>() {
override fun buildModels(data1: List<String>, data2: Boolean) {
MyTextViewModel_().id(1).titleText("first title")
MyTextViewModel_().id(2).titleText("second title")
MyTextViewModel_().id(3).titleText("third title")
}
}
MyTextViewModel.kt
@EpoxyModelClass(layout = R.layout.textview_row)
abstract class MyTextViewModel(): EpoxyModelWithHolder<VH>() {
@EpoxyAttribute
var titleText: String = "Default Title Text"
override fun bind(holder: VH) {
super.bind(holder)
holder.titleTextView.text= titleText
}
class VH : EpoxyHolder() {
lateinit var titleTextView: TextView
override fun bindView(itemView: View) {
titleTextView= itemView.findViewById(R.id.title)
}
}
}
textview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="SIMPLE TITLE"
app:layout_constraintBaseline_toBaselineOf="@+id/more_textview"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/more_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="More"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>