如何使用 Kotlin 视图绑定访问另一个 class 内部的视图
How to access a view inside of another class using Kotlins view binding
我目前正在关注 java 中的 "written" 关于 RecyclerView 的教程。
现在我正在尝试基本上学习教程并用 Kotlin 编写代码。
我现在想从单独创建的 class "RecyclerViewAdapter".
中访问 layout_listitem.xml 中的视图(基本上只是描述了 recyclerview 中单个元素的结构)
这实际上应该不适用于单独的 xml 文件,还是这是一个实际问题?
PS: 我也试过用
"import kotlinx.android.synthetic.main.layout_listitem.*",但这似乎也不起作用。
RecyclerViewAdapter
package com.gmail.mynamepackage.recyclerviewdemo
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
class RecyclerViewAdapter : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
//In this complete class i am not able to access the views directly
}
layout_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="@+id/parentLayout">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/image"
android:src="@mipmap/ic_launcher"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Canada"
want to acc: android:id="@+id/image_name"
android:layout_toRightOf="@+id/image"
android:textColor="#000"
android:layout_centerVertical="true"
android:layout_marginLeft="58dp"
android:textSize="17sp"
/>
</RelativeLayout>
尝试import kotlinx.android.synthetic.main.layout_listitem.view.*
如果我们想在View
上调用合成属性,我们还应该导入 view.*
。
所以基本上发生的事情是,我查看了 RecyclerView class 并且偶然发现了一个字段 itemView,它基本上允许您访问 RecyclerView 节点的视图。
我目前正在关注 java 中的 "written" 关于 RecyclerView 的教程。 现在我正在尝试基本上学习教程并用 Kotlin 编写代码。 我现在想从单独创建的 class "RecyclerViewAdapter".
中访问 layout_listitem.xml 中的视图(基本上只是描述了 recyclerview 中单个元素的结构)这实际上应该不适用于单独的 xml 文件,还是这是一个实际问题?
PS: 我也试过用 "import kotlinx.android.synthetic.main.layout_listitem.*",但这似乎也不起作用。
RecyclerViewAdapter
package com.gmail.mynamepackage.recyclerviewdemo
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
class RecyclerViewAdapter : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
//In this complete class i am not able to access the views directly
}
layout_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="@+id/parentLayout">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/image"
android:src="@mipmap/ic_launcher"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Canada"
want to acc: android:id="@+id/image_name"
android:layout_toRightOf="@+id/image"
android:textColor="#000"
android:layout_centerVertical="true"
android:layout_marginLeft="58dp"
android:textSize="17sp"
/>
</RelativeLayout>
尝试import kotlinx.android.synthetic.main.layout_listitem.view.*
如果我们想在View
上调用合成属性,我们还应该导入 view.*
。
所以基本上发生的事情是,我查看了 RecyclerView class 并且偶然发现了一个字段 itemView,它基本上允许您访问 RecyclerView 节点的视图。