0

I am currently following a Tutorial about RecyclerView, which is "written" in java. Now I am trying to basically take the tutorial and code it in Kotlin. I now want to access Views from the layout_listitem.xml (which basically just describes the structure of a single element in the recyclerview) from a separately created class "RecyclerViewAdapter".

Is this actually supposed not to work with separate xml files or is this an actual problem?

PS: I also tried to use "import kotlinx.android.synthetic.main.layout_listitem.*", but this didn't seem to work either.

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>
Matthias Hofmarcher
  • 384
  • 1
  • 3
  • 11

2 Answers2

1

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

If we want to call the synthetic properties on View, we should also import view.*.

farhanjk
  • 1,652
  • 16
  • 17
  • This sadly did not work, even after rebuilding. I will try to recreate the project, there must be a problem that prevents this class from "seeing" the properties in layout_listitem.view. – Matthias Hofmarcher Apr 15 '19 at 07:49
0

So basically what happened is, I looked into the RecyclerView class and by accident I found a field itemView, which basically lets you access the Views of your RecyclerView Nodes.

Matthias Hofmarcher
  • 384
  • 1
  • 3
  • 11