【问题标题】:How to access a view inside of another class using Kotlins view binding如何使用 Kotlin 视图绑定访问另一个类中的视图
【发布时间】:2019-04-14 07:44:28
【问题描述】:

我目前正在关注一个关于 RecyclerView 的教程,它是用 java “编写”的。 现在我正在尝试基本上学习本教程并在 Kotlin 中编写代码。 我现在想从单独创建的类“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>

【问题讨论】:

    标签: android kotlin android-recyclerview android-view kotlin-extension


    【解决方案1】:

    试试import kotlinx.android.synthetic.main.layout_listitem.view.*

    如果我们要调用View 上的合成属性,我们还应该导入 view.*

    【讨论】:

    • 遗憾的是,即使在重建之后,这也不起作用。我将尝试重新创建项目,一定有问题阻止此类“看到”layout_listitem.view 中的属性。
    【解决方案2】:

    所以基本上发生的事情是,我查看了 RecyclerView 类,偶然发现了一个字段 itemView,它基本上可以让您访问 RecyclerView 节点的视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 2014-08-12
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多