我怎樣才能使用科特林如下java代碼設置佈局管理器來RecycleView我設置佈局管理器來RecycleView:如何使用科特林
mRecyclerView.setLayoutManager(mLinearLayoutManager);
我怎樣才能使用科特林如下java代碼設置佈局管理器來RecycleView我設置佈局管理器來RecycleView:如何使用科特林
mRecyclerView.setLayoutManager(mLinearLayoutManager);
您可以使用綜合性能爲科特林
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'
你可以這樣做
val linearLayoutManager = LinearLayoutManager(this)
linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
recyclerview!!.layoutManager = linearLayoutManager
recyclerview!!.isNestedScrollingEnabled = true
recyclerview!!.setHasFixedSize(true)
簡單地寫這個設置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
我有同樣的問題,原因是我有初始化recyclerView作爲
var recyclerView = findViewById<View>(R.id.recycleView)
確保您初始化如下
var recyclerView = findViewById<View>(R.id.recycleView) as RecyclerView
應用插件,在您的應用程序建立
apply plugin: 'kotlin-android-extensions'
對於我來說,視圖RecyclerView
的編號是my_recycler_view
。
在你的java文件寫入 -
my_recycler_view.layoutManager = LinearLayoutManager(context)
默認LinearLayoutManager(context)
將設置垂直方向,更新按需要。
recyclerView.layoutManager = LinearLayoutManager(context)
或
recyclerView.layoutManager = GridLayoutManager(context, spanCount)
這個答案並不提供比已經給出的答案移動信息。 – Guenther
您可以使用此代碼設置:
binding.recyclerView.setHasFixedSize(true)
binding.recyclerView.layoutManager = LinearLayoutManager(this ,LinearLayoutManager.VERTICAL ,false)
binding.recyclerView.adapter = customAdapter(this ,getList())
recyclerView.layoutManager = mLinearLayoutManager –