将findViewById从Java转换为Kotlin。

huangapple go评论79阅读模式
英文:

Convert findViewById from Java to Kotlin

问题

我正在尝试将这段Java代码转换为Kotlin:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    view = inflater.inflate(R.layout.main_fragment, container, false) as Nothing?
    genre[0] = this.view.findViewById(R.id.MovieGenre)
    genre[1] = this.view.findViewById(R.id.MovieGenre2)
    genre[2] = view.findViewById(R.id.MovieGenre3)
    recyclerView[0] = view.findViewById(R.id.rc_view)
    recyclerView[1] = view.findViewById(R.id.rc_view2)
    recyclerView[2] = view.findViewById(R.id.rc_view3)
    
    if (movieList.size == 0) loadMovie() else refreshList()
    return view
}

问题在于编译器无法识别 findViewById。如何将XML中的每个变量分配给对应的变量?

英文:

I'm trying to convert this piece of code from Java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.main_fragment, container, false);
    genre[0] = view.findViewById(R.id.MovieGenre);
    genre[1] = view.findViewById(R.id.MovieGenre2);
    genre[2] = view.findViewById(R.id.MovieGenre3);
    recyclerView[0] = view.findViewById(R.id.rc_view);
    recyclerView[1] = view.findViewById(R.id.rc_view2);
    recyclerView[2] = view.findViewById(R.id.rc_view3);

    if (movieList.size()==0) loadMovie();
    else refreshList();

    return view;
}

to Kotlin:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    view = inflater.inflate(R.layout.main_fragment, container, false) as Nothing?
    genre[0] = this.view.findViewById(R.id.MovieGenre)
    genre[1] = this.view.findViewById(R.id.MovieGenre2)
    genre[2] = view.findViewById(R.id.MovieGenre3)
    recyclerView[0] = view.findViewById(R.id.rc_view)
    recyclerView[1] = view.findViewById(R.id.rc_view2)
    recyclerView[2] = view.findViewById(R.id.rc_view3)
    
    if (movieList.size == 0) loadMovie() else refreshList()
    return view
}

The problem is that the compiler does not recognize the findViewById. How can I assign the recycler view in my xml to each variable?

答案1

得分: 2

以下是翻译好的内容:

一些更改应该会起作用,我认为。

从以下代码中删除 as Nothing?

view = inflater.inflate(R.layout.main_fragment, container, false) as Nothing?

这可能是不必要的。

我不明白您在初始化时想要实现的内容,比如 genre[0], genre[1] ..

genre[0] = this.view.findViewById(R.id.MovieGenre)
genre[1] = this.view.findViewById(R.id.MovieGenre2)
genre[2] = view.findViewById(R.id.MovieGenre3)
recyclerView[0] = view.findViewById(R.id.rc_view)
recyclerView[1] = view.findViewById(R.id.rc_view2)
recyclerView[2] = view.findViewById(R.id.rc_view3)

您可以尝试像下面这样:

val recycler_view = findViewById<RecyclerView>(R.id.recycler_view)

或者

val recycler_view: RecyclerView = findViewById(R.id.recycler_view)

或者您可以尝试在 @TemaTre 的回答中提到的扩展函数。

或者 您可以使用 anko 库

编辑

Anko 已被弃用 参见

英文:

Couple of changes will work, I think.

Remove as Nothing? from

view = inflater.inflate(R.layout.main_fragment, container, false) as Nothing?

That may be unnecessary.

I don't get what you are trying to achieve here on initialising like genre[0], genre[1] ..

genre[0] = this.view.findViewById(R.id.MovieGenre)
genre[1] = this.view.findViewById(R.id.MovieGenre2)
genre[2] = view.findViewById(R.id.MovieGenre3)
recyclerView[0] = view.findViewById(R.id.rc_view)
recyclerView[1] = view.findViewById(R.id.rc_view2)
recyclerView[2] = view.findViewById(R.id.rc_view3)

instead you can try like below

val recycler_view = findViewById&lt;RecyclerView&gt;(R.id.recycler_view)

or

val recycler_view: RecyclerView = findViewById(R.id.recycler_view)

or you may try extension functions as mentioned in the @TemaTre answer.

or <strike>you may use anko library</strike>

EDIT

Anko is deprecated see

答案2

得分: 1

根据这个链接 https://antonioleiva.com/kotlin-android-extensions/
在方法中不需要使用"findViewById"。现在你可以将名称用作类的字段。例如:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/welcomeMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Hello World!"/>

</FrameLayout>

现在你可以像这样使用 welcomeMessage:

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

    welcomeMessage.text = "Hello Kotlin!"
}
英文:

According to this https://antonioleiva.com/kotlin-android-extensions/
You did not need in method "findViewById". Currently you can use name as a field of class. For example

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;FrameLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/welcomeMessage&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;center&quot;
        android:text=&quot;Hello World!&quot;/&gt;

&lt;/FrameLayout&gt;

And now you can use welcomeMessage like this

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

    welcomeMessage.text = &quot;Hello Kotlin!&quot;
}

huangapple
  • 本文由 发表于 2020年3月16日 23:25:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/60708721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定