【问题标题】:How can i set layoutmanager to RecycleView using kotlin如何使用 kotlin 将 layoutmanager 设置为 RecycleView
【发布时间】:2017-06-13 03:03:10
【问题描述】:

如何使用 kotlin 将 layoutmanager 设置为 RecycleView,如下面的 java 代码:

mRecyclerView.setLayoutManager(mLinearLayoutManager);

【问题讨论】:

  • recyclerView.layoutManager = mLinearLayoutManager

标签: android kotlin


【解决方案1】:

以下两条线将 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)

【讨论】:

    【解决方案2】:

    你可以使用

    recyclerView.layoutManager = LinearLayoutManager(context) // default orientation is vertical
    
    // if you want horizontal recyclerview
    // recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
    

    【讨论】:

      【解决方案3】:

      您可以尝试使用以下解决方案

      val mRecyclerView= v.findViewById<RecyclerView>(R.id.rec) //id RecyclerView    
      mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
      

      【讨论】:

        【解决方案4】:

        你可以这样做

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

        【讨论】:

          【解决方案5】:

          对 AndroidX 使用 RecyclerView.HORIZONTAL 而不是 LinearLayoutManager.HORIZONTAL

          var vegetableList: RecyclerView = findViewById(R.id.list_vegetable)
                      vegetableList.layoutManager = LinearLayoutManager(this,
                      RecyclerView.HORIZONTAL, false)
          

          【讨论】:

            【解决方案6】:

            只需写这个来设置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
            

            【讨论】:

              【解决方案7】:

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

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

              确保你初始化如下

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

              【讨论】:

                【解决方案8】:

                在您的应用构建中应用插件

                 apply plugin: 'kotlin-android-extensions'
                

                对于我的情况,RecyclerView 的视图 ID 是 my_recycler_view。

                在你的 java 文件中写 -

                my_recycler_view.layoutManager = LinearLayoutManager(context)
                

                默认LinearLayoutManager(context)会设置垂直方向,根据需要更新。

                【讨论】:

                  【解决方案9】:

                  您可以使用此代码进行设置:

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

                  【讨论】:

                    【解决方案10】:
                    private var mRecyclerView: RecyclerView? = null
                    

                    mRecyclerView?.layoutManager = LinearLayoutManager(activity)

                    【讨论】:

                      【解决方案11】:

                      选择布局:

                      • 线性布局管理器(上下文)。 // 垂直
                      • LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) // 水平的
                      • GridLayoutManager(context, numberOfColumns) // 网格

                      然后使用 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"
                      

                      有关详细信息,请参阅:here 和 here。

                      【讨论】:

                        【解决方案12】:

                        如果您使用的是 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 是检查当前选择了哪个行项的索引。

                        【讨论】:

                          【解决方案13】:

                          recyclerView.layoutManager = LinearLayoutManager(context)

                          或

                          recyclerView.layoutManager = GridLayoutManager(context, spanCount)

                          【讨论】:

                          • 这个答案没有提供移动信息而不是已经给出的答案。
                          猜你喜欢
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2018-06-15
                          • 1970-01-01
                          相关资源
                          最近更新 更多