[英]Convert findViewById from Java to Kotlin
I'm trying to convert this piece of code from Java:我正在尝试从 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.问题是编译器无法识别 findViewById。 How can I assign the recycler view in my xml to each variable?
如何将 xml 中的回收器视图分配给每个变量?
Couple of changes will work, I think.我想,有一些变化会奏效。
Remove as Nothing?
删除
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], 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<RecyclerView>(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.或者您可以尝试@TemaTre 答案中提到的扩展功能。
or
you may use
anko library
或者
你可以使用
anko 库
EDIT编辑
According to this https://antonioleiva.com/kotlin-android-extensions/ You did not need in method "findViewById".根据这个https://antonioleiva.com/kotlin-android-extensions/你不需要方法“findViewById”。 Currently you can use name as a field of class.
目前,您可以使用名称作为类的字段。 For example
例如
<?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>
And now you can use welcomeMessage like this现在你可以像这样使用welcomeMessage
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
welcomeMessage.text = "Hello Kotlin!"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.