将findViewById从Java转换为Kotlin。

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

Convert findViewById from Java to Kotlin

问题

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

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

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

英文:

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

  1. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  2. view = inflater.inflate(R.layout.main_fragment, container, false);
  3. genre[0] = view.findViewById(R.id.MovieGenre);
  4. genre[1] = view.findViewById(R.id.MovieGenre2);
  5. genre[2] = view.findViewById(R.id.MovieGenre3);
  6. recyclerView[0] = view.findViewById(R.id.rc_view);
  7. recyclerView[1] = view.findViewById(R.id.rc_view2);
  8. recyclerView[2] = view.findViewById(R.id.rc_view3);
  9. if (movieList.size()==0) loadMovie();
  10. else refreshList();
  11. return view;
  12. }

to Kotlin:

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

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?

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

这可能是不必要的。

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

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

您可以尝试像下面这样:

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

或者

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

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

或者 您可以使用 anko 库

编辑

Anko 已被弃用 参见

英文:

Couple of changes will work, I think.

Remove as Nothing? from

  1. 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] ..

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

instead you can try like below

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

or

  1. 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"。现在你可以将名称用作类的字段。例如:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:id="@+id/welcomeMessage"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center"
  11. android:text="Hello World!"/>
  12. </FrameLayout>

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

  1. override fun onCreate(savedInstanceState: Bundle?) {
  2. super.onCreate(savedInstanceState)
  3. setContentView(R.layout.activity_main)
  4. welcomeMessage.text = "Hello Kotlin!"
  5. }
英文:

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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;FrameLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;&gt;
  6. &lt;TextView
  7. android:id=&quot;@+id/welcomeMessage&quot;
  8. android:layout_width=&quot;wrap_content&quot;
  9. android:layout_height=&quot;wrap_content&quot;
  10. android:layout_gravity=&quot;center&quot;
  11. android:text=&quot;Hello World!&quot;/&gt;
  12. &lt;/FrameLayout&gt;

And now you can use welcomeMessage like this

  1. override fun onCreate(savedInstanceState: Bundle?) {
  2. super.onCreate(savedInstanceState)
  3. setContentView(R.layout.activity_main)
  4. welcomeMessage.text = &quot;Hello Kotlin!&quot;
  5. }

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:

确定