[英]Kotlin - RecyclerView.LayoutManager issue
I'm learning Kotlin
& Adroid Studio and recently I have stuck on RecyclerView
.我正在学习
Kotlin
和 Adroid Studio,最近我坚持使用RecyclerView
。
The problem is that I'm not able to go forward due to error showing on LayoutManager
.问题是由于
LayoutManager
显示的错误,我无法继续。
My little code looks like:我的小代码看起来像:
package com.example.julian.recyclerviewapplication
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
RecyclerView.LayoutManager=LinearLayoutManager(this)
}
}
LayoutManager
is underlined and tells me that: " LayoutManager
下划线并告诉我:“
Classifier 'LayoutManager' does not have a companion object, and thus must be initalized here".
I'm not a advanced programist, rather a newbie and I've been trying to fix this remark but at this moment its beyond my skills.我不是高级程序员,而是新手,我一直在努力解决这个问题,但此时它超出了我的技能。
Thank you for your help.感谢您的帮助。
val layoutManager = LinearLayoutManager(this)
<your recyclerview>.layoutManager = layoutManager
Or even shorter:或者更短:
<your recyclerview>.layoutManager = LinearLayoutManager(this)
You can do like below:你可以像下面这样做:
my_recyclerview_id.layoutManager = LinearLayoutManager(this);
Here "my_recyclerview_id"
is your recyclerview
id from your layout XML file.这里的
"my_recyclerview_id"
是布局 XML 文件中的recyclerview
id。
Try this 1st way试试这个第一种方式
val layoutManager = LinearLayoutManager(applicationContext)
your_recyclerview_id!!.layoutManager = layoutManager
your_recyclerview_id!!.adapter = Your_adapter(applicationContext)
2nd way第二种方式
your_recyclerview_id!!.layoutManager=LinearLayoutManager(applicationContext)
your_recyclerview_id!!.adapter = Your_adapter(applicationContext);
your_recyclerview_id!!.adapter.notifyDataSetChanged()
Best of luck with Kotlin祝 Kotlin 好运
No need to define 'findviewByID' in kotlin...Android understand by default all resource and View element.无需在 kotlin 中定义 'findviewByID'...Android 默认理解所有资源和 View 元素。
you can try it...你可以试试看...
Only use this code when you are working on RecycleView in kotlin只有在 kotlin 中处理 RecycleView 时才使用此代码
rc_all_transaction.setLayoutManager(LinearLayoutManager(activity,
LinearLayoutManager.VERTICAL, true))
here rc_all_transaction is ID of Recycleview and rest is setting Layout Manager这里 rc_all_transaction 是 Recycleview 的 ID,其余是设置布局管理器
Thanks谢谢
Tt should be RecyclerView.layoutManager = LinearLayoutManager(this)
. Tt 应该是
RecyclerView.layoutManager = LinearLayoutManager(this)
。 Lower-case l
and not upper-case L
.小写
l
而不是大写L
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.