简体   繁体   English

Kotlin - RecyclerView.LayoutManager 问题

[英]Kotlin - RecyclerView.LayoutManager issue

I'm learning Kotlin & Adroid Studio and recently I have stuck on RecyclerView .我正在学习Kotlin和 Adroid Studio,最近我坚持使用RecyclerView

The problem is that I'm not able to go forward due to error showing on LayoutManager .问题是由于LayoutManager显示的错误,我无法继续。

My little code looks like:我的小代码看起来像:

 package com.example.julian.recyclerviewapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        RecyclerView.LayoutManager=LinearLayoutManager(this)

    }
}

LayoutManager is underlined and tells me that: " LayoutManager下划线并告诉我:“

Classifier 'LayoutManager' does not have a companion object, and thus must be initalized here".

I'm not a advanced programist, rather a newbie and I've been trying to fix this remark but at this moment its beyond my skills.我不是高级程序员,而是新手,我一直在努力解决这个问题,但此时它超出了我的技能。

Thank you for your help.感谢您的帮助。

val layoutManager = LinearLayoutManager(this)
<your recyclerview>.layoutManager = layoutManager

Or even shorter:或者更短:

<your recyclerview>.layoutManager = LinearLayoutManager(this)

You can do like below:你可以像下面这样做:

my_recyclerview_id.layoutManager = LinearLayoutManager(this);

Here "my_recyclerview_id" is your recyclerview id from your layout XML file.这里的"my_recyclerview_id"是布局 XML 文件中的recyclerview id。

Try this 1st way试试这个第一种方式

val layoutManager = LinearLayoutManager(applicationContext)
your_recyclerview_id!!.layoutManager = layoutManager
your_recyclerview_id!!.adapter = Your_adapter(applicationContext)

2nd way第二种方式

 your_recyclerview_id!!.layoutManager=LinearLayoutManager(applicationContext)
 your_recyclerview_id!!.adapter = Your_adapter(applicationContext);
 your_recyclerview_id!!.adapter.notifyDataSetChanged()

Best of luck with Kotlin祝 Kotlin 好运

No need to define 'findviewByID' in kotlin...Android understand by default all resource and View element.无需在 kotlin 中定义 'findviewByID'...Android 默认理解所有资源和 View 元素。

you can try it...你可以试试看...

Only use this code when you are working on RecycleView in kotlin只有在 kotlin 中处理 RecycleView 时才使用此代码

 rc_all_transaction.setLayoutManager(LinearLayoutManager(activity, 
   LinearLayoutManager.VERTICAL, true))

here rc_all_transaction is ID of Recycleview and rest is setting Layout Manager这里 rc_all_transaction 是 Recycleview 的 ID,其余是设置布局管理器

Thanks谢谢

Tt should be RecyclerView.layoutManager = LinearLayoutManager(this) . Tt 应该是RecyclerView.layoutManager = LinearLayoutManager(this) Lower-case l and not upper-case L .小写l而不是大写L

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter等效RecyclerView.LayoutManager - Flutter equivalent RecyclerView.LayoutManager 在自定义LayoutManager中(扩展了RecyclerView.LayoutManager)RecyclerView.getChildViewHolderInt NullPointerException问题 - In custom LayoutManager (extends RecyclerView.LayoutManager) RecyclerView.getChildViewHolderInt NullPointerException issue RecyclerView.LayoutManager 不显示视图 - RecyclerView.LayoutManager doesn't show view RecyclerView、RecyclerView.Adapter和RecyclerView.LayoutManager在哪里声明? - Where to declare RecyclerView, RecyclerView.Adapter and RecyclerView.LayoutManager? 为什么我在 recyclerView.layoutmanager=linearlayout 中出现错误 - why i am getting error in recyclerView.layoutmanager=linearlayout 有人可以解释 RecyclerView.LayoutManager 返回的 getItemCount 方法是什么吗? - Can someone explain what getItemCount method in RecyclerView.LayoutManager return? 如何从RecyclerView.LayoutManager中获取当前的RecyclerView.Adapter? - How do you get the current RecyclerView.Adapter from within a RecyclerView.LayoutManager? 如何为自定义 RecyclerView.LayoutManager 实现的 LayoutParams 类定义自定义属性? - How do you define custom attributes for the LayoutParams class of a custom RecyclerView.LayoutManager implementation? Recyclerview 在调用 LayoutManager 时不断在 Fragment 中崩溃 - Kotlin - Recyclerview keep crashing in Fragment when calling LayoutManager - Kotlin Kotlin 编程语言中的 Android Studio 错误(RecyclerView 没有 LayoutManager) - Android Studio Error (RecyclerView has no LayoutManager) in Kotlin programing language
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM