2017-06-13 102 views
1

我怎样才能使用科特林如下java代码设置布局管理器来RecycleView我设置布局管理器来RecycleView:如何使用科特林

mRecyclerView.setLayoutManager(mLinearLayoutManager); 
+0

recyclerView.layoutManager = mLinearLayoutManager –

回答

4

您可以使用综合性能为科特林

mRecyclerView.layoutManager = LinearLayoutManager(context) 

LinearLayoutManager constructors

为了做到这一点,请将以下文件添加到您的根级build.gradle文件中:

buildscript { 
    dependencies { 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" 
    } 
} 

然后在您的应用程序的build.gradle添加以下顶部:

apply plugin: 'kotlin-android-extensions'

0

你可以这样做

val linearLayoutManager = LinearLayoutManager(this) 
linearLayoutManager.orientation = LinearLayoutManager.VERTICAL 
recyclerview!!.layoutManager = linearLayoutManager 
recyclerview!!.isNestedScrollingEnabled = true 
recyclerview!!.setHasFixedSize(true) 
2

简单地写这个设置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 
0

我有同样的问题,原因是我有初始化recyclerView作为

var recyclerView = findViewById<View>(R.id.recycleView) 

确保您初始化如下

var recyclerView = findViewById<View>(R.id.recycleView) as RecyclerView 
0

应用插件,在您的应用程序建立

apply plugin: 'kotlin-android-extensions' 

对于我来说,视图RecyclerView的编号是my_recycler_view

在你的java文件写入 -

my_recycler_view.layoutManager = LinearLayoutManager(context) 

默认LinearLayoutManager(context)将设置垂直方向,更新按需要。

-1

recyclerView.layoutManager = LinearLayoutManager(context)

recyclerView.layoutManager = GridLayoutManager(context, spanCount)

+1

这个答案并不提供比已经给出的答案移动信息。 – Guenther

0

您可以使用此代码设置:

binding.recyclerView.setHasFixedSize(true) 
binding.recyclerView.layoutManager = LinearLayoutManager(this ,LinearLayoutManager.VERTICAL ,false) 
binding.recyclerView.adapter = customAdapter(this ,getList())