【发布时间】:2017-06-13 03:03:10
【问题描述】:
如何使用 kotlin 将 layoutmanager 设置为 RecycleView,如下面的 java 代码:
mRecyclerView.setLayoutManager(mLinearLayoutManager);
【问题讨论】:
-
recyclerView.layoutManager = mLinearLayoutManager
如何使用 kotlin 将 layoutmanager 设置为 RecycleView,如下面的 java 代码:
mRecyclerView.setLayoutManager(mLinearLayoutManager);
【问题讨论】:
以下两条线将 orientation 设置为 vertical
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL ,false)
或
mRecyclerView.layoutManager = LinearLayoutManager(this)
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL ,false)
设置水平方向
要设置网格布局,
mRecyclerView.layoutManager = GridLayoutManager(this, spanCount)
【讨论】:
你可以使用
recyclerView.layoutManager = LinearLayoutManager(context) // default orientation is vertical
// if you want horizontal recyclerview
// recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
【讨论】:
您可以尝试使用以下解决方案
val mRecyclerView= v.findViewById<RecyclerView>(R.id.rec) //id RecyclerView
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
【讨论】:
你可以这样做
val linearLayoutManager = LinearLayoutManager(this)
linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
recyclerview!!.layoutManager = linearLayoutManager
recyclerview!!.isNestedScrollingEnabled = true
recyclerview!!.setHasFixedSize(true)
【讨论】:
对 AndroidX 使用 RecyclerView.HORIZONTAL 而不是 LinearLayoutManager.HORIZONTAL
var vegetableList: RecyclerView = findViewById(R.id.list_vegetable)
vegetableList.layoutManager = LinearLayoutManager(this,
RecyclerView.HORIZONTAL, false)
【讨论】:
只需写这个来设置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 的视图 ID 是 my_recycler_view。
在你的 java 文件中写 -
my_recycler_view.layoutManager = LinearLayoutManager(context)
默认LinearLayoutManager(context)会设置垂直方向,根据需要更新。
【讨论】:
您可以使用此代码进行设置:
binding.recyclerView.setHasFixedSize(true)
binding.recyclerView.layoutManager = LinearLayoutManager(this ,LinearLayoutManager.VERTICAL ,false)
binding.recyclerView.adapter = customAdapter(this ,getList())
【讨论】:
private var mRecyclerView: RecyclerView? = null
mRecyclerView?.layoutManager = LinearLayoutManager(activity)
【讨论】:
选择布局:
然后使用 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"
【讨论】:
如果您使用的是 Kotlin android。
声明lateinit变量smoothScroller
lateinit var smoothScroller: SmoothScroller
在 OnCreate 方法中初始化 smoothScroller
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
smoothScroller = object : LinearSmoothScroller(context) {
override fun getVerticalSnapPreference(): Int {
return SNAP_TO_START
}
}
}
最后,检查 Adopter 是否已初始化? 提交旧数据集更改(如果有)。 设置位置。 启动滚动条。
if (this@ChooseTemplate::genericAdapter.isInitialized && this@ChooseTemplate::smoothScroller.isInitialized) {
this@ChooseTemplate.genericAdapter!!.notifyDataSetChanged()
smoothScroller.setTargetPosition(this@ChooseTemplate.templatesrowIndex);
dataBinding!!.rvTemplate.layoutManager!!.startSmoothScroll(this@ChooseTemplate.smoothScroller);
}
dataBinding 是一个绑定对象。 rvTemplate 是一个回收器视图。 genericadapter 是 rvTemplate 的采用者。 templatesrowIndex 是检查当前选择了哪个行项的索引。
【讨论】:
recyclerView.layoutManager = LinearLayoutManager(context)
或
recyclerView.layoutManager = GridLayoutManager(context, spanCount)
【讨论】: