如何使用kotlin将layoutmanager设置为RecycleView作为以下java代码:
mRecyclerView.setLayoutManager(mLinearLayoutManager);
答案 0 :(得分:11)
以下两行将 方向 设置为 垂直
local key
OR
public function floors()
{
return $this->hasMany(\Otellier\Floor::class, 'hotel_id', 'hotel_id');
}
设置水平方向
设置网格布局
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL ,false)
答案 1 :(得分:6)
您可以使用Kotlin的合成属性
/
LinearLayoutManager constructors
为此,请在您的根级from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include("polls.urls")) # add / after polls
]
文件中添加以下内容:
mRecyclerView.layoutManager = LinearLayoutManager(context)
然后在您的应用程序中build.gradle
在顶部添加以下内容:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
答案 2 :(得分:0)
你可以这样做
val linearLayoutManager = LinearLayoutManager(this)
linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
recyclerview!!.layoutManager = linearLayoutManager
recyclerview!!.isNestedScrollingEnabled = true
recyclerview!!.setHasFixedSize(true)
答案 3 :(得分:0)
只需写下此内容即可设置LayoutManager
// Define this globally
lateinit var recyclerView: RecyclerView
// Initialize this after `activity` or `fragment` is created
recyclerView = findViewById(R.id.recyclerView) as RecyclerView
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(activity!!) as RecyclerView.LayoutManager
答案 4 :(得分:0)
我有同样的问题,原因是我将recyclerView初始化为
var recyclerView = findViewById<View>(R.id.recycleView)
确保初始化如下
var recyclerView = findViewById<View>(R.id.recycleView) as RecyclerView
答案 5 :(得分:0)
在您的应用构建中应用插件
apply plugin: 'kotlin-android-extensions'
对于我的案例视图,RecyclerView
的ID为my_recycler_view
。
在你的java文件中写 -
my_recycler_view.layoutManager = LinearLayoutManager(context)
默认情况下,LinearLayoutManager(context)
会设置垂直方向,根据需要进行更新。
答案 6 :(得分:0)
您可以使用以下代码进行设置:
[key: string]: any | KnockoutBindingHandler;
答案 7 :(得分:0)
private var mRecyclerView: RecyclerView? = null
mRecyclerView?.layoutManager = LinearLayoutManager(活动)
答案 8 :(得分:0)
您可以尝试使用以下解决方案
Number.isNaN()
答案 9 :(得分:0)
使用AndroidX的RecyclerView.HORIZONTAL代替LinearLayoutManager.HORIZONTAL
var vegetableList: RecyclerView = findViewById(R.id.list_vegetable)
vegetableList.layoutManager = LinearLayoutManager(this,
RecyclerView.HORIZONTAL, false)
答案 10 :(得分:0)
选择布局:
然后使用 Kotlin 的 apply() 消除重复来应用布局。
val rv = view.findViewById(R.id.recyclerView) as RecyclerView
rv.apply {
layoutManager = LinearLayoutManager(context)
adapter = recyclerViewAdapter()
setHasFixedSize(true)
...
}
也可以像这样在 XML 中设置:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
答案 11 :(得分:-1)
recyclerView.layoutManager = LinearLayoutManager(context)
或
recyclerView.layoutManager = GridLayoutManager(context, spanCount)