我有一个RecycleView
的kotlin使用ArrayList
。这是代码:
class TestProvider(val text: ArrayList<String> , val context:Context): RecyclerView.Adapter<TestProvider.holder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): holder {
return holder(LayoutInflater.from(context).inflate(R.layout.testitem,parent,false))
}
override fun getItemCount(): Int {
return text.size
}
override fun onBindViewHolder(holder: holder, position: Int) {
holder.txtText.text=text[position]
}
class holder(item:View): RecyclerView.ViewHolder(item) {
var txtText:TextView=item.findViewById(R.id.txtTest)
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtTest"/>
</LinearLayout>
var task1="abdef"
internal var dateStart: Date? = null
internal var dateEnd: Date? = null
var taskProvider: Provider= Provider()
var TaskList= ArrayList<String>()
lateinit var taskAdapter: TestProvider
TaskList.add(0,task1)
TaskList.add(task1)
TaskList.add(task1)
taskAdapter = TestProvider(TaskList,this)
rcvTest.addItemDecoration(dividerItemDecoration)
rcvTest.layoutManager = linearLayoutManager
rcvTest.setHasFixedSize(true)
rcvTest.adapter = taskAdapter
taskAdapter.notifyDataSetChanged()
RecycleView
中的文本的大小始终为= 0。
答案 0 :(得分:1)
我尝试重建您的应用程序,并体验到您的适配器似乎是错误的。 请将类声明更改为:
class TestProvider(val text: ArrayList<String>, val context: Context) : RecyclerView.Adapter<TestProvider.holder>() {
另外,您的testitem布局的高度应为wrap_content,因为一项会占据整个屏幕,否则