Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在尝试在Kotlin中创建简单的recyclerView,但适配器应用不正确_Android_Android Recyclerview_Kotlin - Fatal编程技术网

Android 正在尝试在Kotlin中创建简单的recyclerView,但适配器应用不正确

Android 正在尝试在Kotlin中创建简单的recyclerView,但适配器应用不正确,android,android-recyclerview,kotlin,Android,Android Recyclerview,Kotlin,我试图在Kotlin中创建一个简单的recyclerView,其中包含我通过Volley获得的数据(我已经确认返回了正确的数据),我一直运行到错误E/recyclerView:未连接适配器;跳过布局,事实上我已使用创建的自定义适配器类指定了适配器: class ImageAdapter(var c: Context, var list: ArrayList<Image>) : RecyclerView.Adapter<ImageAdapter.ViewHolder>()

我试图在Kotlin中创建一个简单的recyclerView,其中包含我通过Volley获得的数据(我已经确认返回了正确的数据),我一直运行到错误
E/recyclerView:未连接适配器;跳过布局
,事实上我已使用创建的自定义适配器类指定了适配器:

class ImageAdapter(var c: Context, var list: ArrayList<Image>) : RecyclerView.Adapter<ImageAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder? {
    val layoutInflater = LayoutInflater.from(parent.context)
    return ViewHolder(layoutInflater.inflate(R.layout.image_cardview, parent, false))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val imageUrl = list[position].url
    val submitter = list[position].submitter
    val color = list[position].color
    holder.submitterTV.text = submitter
    holder.card.setCardBackgroundColor(Color.parseColor(color))
}

override fun getItemCount() = list.size
class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
    val card = itemView.card
    val submitterTV = itemView.submitter
    val imageView = itemView.image
}

}
类ImageAdapter(var c:Context,var list:ArrayList):RecyclerView.Adapter(){
覆盖创建ViewHolder(父:ViewGroup,viewType:Int):ViewHolder{
val layoutInflater=layoutInflater.from(parent.context)
返回视图支架(布局更平坦。充气(R.layout.image_cardview,父视图,假))
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val imageUrl=list[position].url
val submitter=列表[位置].提交者
val color=列表[位置].color
holder.submitterTV.text=提交者
holder.card.setCardBackgroundColor(Color.parseColor(Color))
}
覆盖getItemCount()=list.size
类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val card=itemView.card
val submitterTV=itemView.submitter
val imageView=itemView.image
}
}
这是我的MainActivty类,我在其中实际调用JSON并尝试将适配器与我创建的ArrayList连接起来:

class MainActivity : AppCompatActivity() {
val images = ArrayList<Image>()
override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    imageList.layoutManager = LinearLayoutManager(applicationContext)
    request("https://api.unsplash.com/photos/curated/?client_id=API_KEY")
    imageList.adapter = ImageAdapter(applicationContext, images)

}

private fun request(url: String) {
    val queue = Volley.newRequestQueue(this)
    val stringRequest = JsonArrayRequest(url, Response.Listener<JSONArray> { response ->
        try {
            for (i in 0..(response.length() - 1)) {
                val image: Image = Image(response.getJSONObject(i).getJSONObject("urls").getString("full"), response.getJSONObject(i).getJSONObject("user").getString("username"), response.getJSONObject(i).getString("color"))
                images.add(image)
            }

            imageList.adapter.notifyDataSetChanged()

        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }, Response.ErrorListener { })
    queue.add(stringRequest)
}
}
class MainActivity:AppCompatActivity(){
val images=ArrayList()
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imageList.layoutManager=LinearLayoutManager(applicationContext)
请求(“https://api.unsplash.com/photos/curated/?client_id=API_KEY")
imageList.adapter=ImageAdapter(applicationContext,图像)
}
私人娱乐请求(url:String){
val queue=Volley.newRequestQueue(此)
val stringRequest=JsonArrayRequest(url,Response.Listener{Response->
试一试{
对于(0中的i..(response.length()-1)){
val image:image=image(response.getJSONObject(i).getJSONObject(“url”).getString(“完整”)、response.getJSONObject(i).getJSONObject(“用户”).getString(“用户名”)、response.getJSONObject(i).getString(“颜色”))
图像。添加(图像)
}
imageList.adapter.notifyDataSetChanged()
}捕获(e:JSONException){
e、 printStackTrace()
}
},Response.ErrorListener{})
添加(stringRequest)
}
}

我创建了一个自定义数据类
Image
,它存储三个字段:imageUrl、submitter和color,所有这些字段都来自JSON。我认为在请求完成后在我的适配器上调用
notifyDataSetChanged()
,将允许recyclerView得到更新并显示项目,但屏幕上根本没有显示任何内容。有人知道我把事情搞砸了吗?

看看下面的例子,我想这可以给你一个想法

您可以从这里获得示例:

主要活动

class MainActivity : AppCompatActivity() {
private var myAdapter: MyAdapter? = null
private var arrayList: ArrayList<MyItem>? = null
private var layoutManager: RecyclerView.LayoutManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    initialize()
    setupList()
    loaddata()
}

private fun loaddata() {

    for (i in 0..9) {
        val myItem = MyItem()
        myItem.name = "Sid_" + i
        myItem.number = "098899876" + i
        arrayList!!.add(myItem)
    }

    myAdapter!!.notifyDataSetChanged()

}

private fun setupList() {
    rlItems!!.layoutManager = layoutManager
    rlItems!!.adapter = myAdapter
}

private fun initialize() {
    arrayList = ArrayList<MyItem>()
    layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
    myAdapter = MyAdapter(arrayList!!, this, R.layout.item_cell)

  }
}
    private fun setUpAdapter() {
    adapter = AddressAdapter(fun(item: AddressData, position: Int, type: Int) {
        when (type) {
            R.id.tvEdit -> {
                context.showToast(
                        "Editbtn" + position,
                        showInReleaseBuild = true
                )
            }
            R.id.tvDelete -> {
                context.showToast(
                        "Deletebtn" + position,
                        showInReleaseBuild = true
                )
            }
            R.id.tvAddress -> {
                context.showToast(
                        "addressbtn" + position,
                        showInReleaseBuild = true
                )
            }

        }


    })
    rvAddress.adapter = adapter
    rvAddress.setEmptyView(llEmptyViewMain)
    val addressData = AddressData(1, "abcd")
    val addressData1 = AddressData(2, "abceeed")


    lstData = listOf(addressData, addressData1)
    adapter.setData(lstData)
}

尝试使用下面的代理适配器是参考链接


参考链接:

您可以从这里下载源代码()

MainActivity.kt:

package com.deepshikha.recyclerviewkotlin
import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.Window
import android.view.WindowManager
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main.*
import net.simplifiedcoding.recyclerviewexample.CustomAdapter

class MainActivity : Activity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

        setContentView(R.layout.activity_main)

        val users = ArrayList<Model_Details>()
        users.add(Model_Details("Kaju katli", "Kaju katli, also known as kaju Katari or kaju barfi, is an Indian dessert similar to a barfi.",R.drawable.kaju))
        users.add(Model_Details("Doughnut", "The doughnut is popular in many countries and prepared in various forms as a sweet snack that can be homemade or purchased in bakeries, supermarkets, food stalls, and franchised specialty outlets",R.drawable.donuts))
        users.add(Model_Details("Panna cotta", "Panna cotta is an Italian dessert of sweetened cream thickened with gelatin and molded. The cream may be aromatized with rum, coffee, vanilla, or other flavorings.",R.drawable.panna_cotta))
        users.add(Model_Details("Rose Cookies", "Rose cooky is a famous South Indian snack made during festivals",R.drawable.rosecookies))
        users.add(Model_Details("Belgian waffle", "In North America, Belgian waffles are a variety of waffle with a lighter batter, larger squares, and deeper pockets than ordinary American waffles",R.drawable.belgianwaffle))

        val obj_adapter = CustomAdapter(users)

        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        recyclerView.adapter = obj_adapter
    }


}
package net.simplifiedcoding.recyclerviewexample

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.deepshikha.recyclerviewkotlin.Model_Details
import com.deepshikha.recyclerviewkotlin.R
import kotlinx.android.synthetic.main.adapter_details.view.*

class CustomAdapter(val userList: ArrayList<Model_Details>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    //this method is returning the view for each item in the list
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.adapter_details, parent, false)
        return ViewHolder(v)
    }

    //this method is binding the data on the list
    override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
        holder.bindItems(userList[position])
    }

    //this method is giving the size of the list
    override fun getItemCount(): Int {
        return userList.size
    }

    //the class is hodling the list view
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindItems(user: Model_Details) {
            itemView.tv_name.text=user.name
            itemView.tv_des.text=user.des
            itemView.iv_name.setImageResource(user.image)

        }
    }

}
package com.deepshikha.recyclererviewkotlin
导入android.app.Activity
导入android.os.Bundle
导入android.support.v7.widget.LinearLayoutManager
导入android.view.Window
导入android.view.WindowManager
导入android.widget.LinearLayout
导入kotlinx.android.synthetic.main.activity\u main*
导入net.simplifiedcoding.recycleServiceExample.CustomAdapter
类MainActivity:Activity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
requestWindowFeature(窗口。功能\u无\u标题)
window.setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏)
setContentView(R.layout.activity_main)
val users=ArrayList()
添加(型号_详情(“卡朱卡特利”,“卡朱卡特利,也称为卡朱卡塔里或卡朱巴菲,是一种类似巴菲的印度甜点。”,R.dravable.Kaju))
用户。添加(Model_详细信息(“甜甜圈”,“甜甜圈在许多国家很受欢迎,以各种形式制成甜食,可在面包房、超市、食品摊和特许专卖店自制或购买”,R.drawable.donuts))
用户。添加(Model_详情(“Panna cotta”,“Panna cotta是一种用明胶增稠并模制的甜奶油制成的意大利甜点。奶油可以用朗姆酒、咖啡、香草或其他调味品调味。”,R.drawable.Panna_cotta))
添加(Model_详细信息(“玫瑰曲奇”,“玫瑰曲奇是节日期间制作的著名南印度小吃”,R.drawable.rosecookies))
用户。添加(型号_详情(“比利时华夫饼”,“在北美,比利时华夫饼是一种比普通美国华夫饼面糊更轻、方形更大、口袋更深的华夫饼”,R.drawable.belgianwaffle))
val obj_适配器=自定义适配器(用户)
recyclerView.layoutManager=LinearLayoutManager(此,LinearLayout.VERTICAL,false)
recyclerView.adapter=obj_适配器
}
}
CustomAdapter.kt:

package com.deepshikha.recyclerviewkotlin
import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.Window
import android.view.WindowManager
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main.*
import net.simplifiedcoding.recyclerviewexample.CustomAdapter

class MainActivity : Activity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

        setContentView(R.layout.activity_main)

        val users = ArrayList<Model_Details>()
        users.add(Model_Details("Kaju katli", "Kaju katli, also known as kaju Katari or kaju barfi, is an Indian dessert similar to a barfi.",R.drawable.kaju))
        users.add(Model_Details("Doughnut", "The doughnut is popular in many countries and prepared in various forms as a sweet snack that can be homemade or purchased in bakeries, supermarkets, food stalls, and franchised specialty outlets",R.drawable.donuts))
        users.add(Model_Details("Panna cotta", "Panna cotta is an Italian dessert of sweetened cream thickened with gelatin and molded. The cream may be aromatized with rum, coffee, vanilla, or other flavorings.",R.drawable.panna_cotta))
        users.add(Model_Details("Rose Cookies", "Rose cooky is a famous South Indian snack made during festivals",R.drawable.rosecookies))
        users.add(Model_Details("Belgian waffle", "In North America, Belgian waffles are a variety of waffle with a lighter batter, larger squares, and deeper pockets than ordinary American waffles",R.drawable.belgianwaffle))

        val obj_adapter = CustomAdapter(users)

        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        recyclerView.adapter = obj_adapter
    }


}
package net.simplifiedcoding.recyclerviewexample

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.deepshikha.recyclerviewkotlin.Model_Details
import com.deepshikha.recyclerviewkotlin.R
import kotlinx.android.synthetic.main.adapter_details.view.*

class CustomAdapter(val userList: ArrayList<Model_Details>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    //this method is returning the view for each item in the list
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.adapter_details, parent, false)
        return ViewHolder(v)
    }

    //this method is binding the data on the list
    override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
        holder.bindItems(userList[position])
    }

    //this method is giving the size of the list
    override fun getItemCount(): Int {
        return userList.size
    }

    //the class is hodling the list view
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindItems(user: Model_Details) {
            itemView.tv_name.text=user.name
            itemView.tv_des.text=user.des
            itemView.iv_name.setImageResource(user.image)

        }
    }

}
package net.simplifiedcoding.recycleriveweb示例
导入android.support.v7.widget.RecyclerView
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget.TextView
导入com.deepshikha.recyclererviewkotlin.Model_详细信息
导入com.deepshikha.recyclererviewkotlin.R
导入kotlinx.android.synthetic.main.adapter_details.view*
类CustomAdapter(val userList:ArrayList):RecyclerView.Adapter(){
//此方法返回列表中每个项目的视图
重写CreateViewHolder(父级:ViewGroup,viewType:Int):CustomAdapter.ViewHolder{
Valv=LayOutButter。从(父上下文)。
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    //rcv is id of Recyclerview
    rcv.layoutManager = LinearLayoutManager(this)
    rcv.adapter = MyAdpater()
}
class MyAdpater : RecyclerView.Adapter<ViewHolder>() {
val arr = listOf("A", "B", "C", "D", "E") //static array
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.textView.setText(arr.get(position)) //set text to textview by position
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return ViewHolder(TextView(parent.context))
 }

override fun getItemCount(): Int {
    return arr.count() //return array count
}}

class ViewHolder(val textView: TextView): RecyclerView.ViewHolder(textView){ //ViewHolder with textview}
class ResultActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.question_list_activity)

        if (intent != null) {
            var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults") as ArrayList<ResultBO>

            if (results.size > 0) {
                recycler_view.adapter = ResultAdapter(results, object : OnRecyclerItemClickListener {
                    override fun onItemClicked(view: View?, position: Int) {
                        /*var intent = Intent(this@SubjectListActivity,McqActivity::class.java)
                        intent.putExtra("keyTagBO",subjects.get(position))
                        startActivity(intent)*/
                    }
                });
            }
        }

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/titleTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dummy Value"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/option1Tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Dummy"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

        <TextView
            android:id="@+id/option2Tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Dummy Value"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/option3Tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Dummy"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

        <TextView
            android:id="@+id/option4Tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Dummy Value"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


    </LinearLayout>
</LinearLayout>
class ResultAdapter(items: List<ResultBO>, onRecyclerItemClickListener: OnRecyclerItemClickListener) : BaseAdapter<ResultBO, ResultViewHolder>(items, onRecyclerItemClickListener) {

    override fun onCreateViewHolder(parent: ViewGroup, pos: Int): ResultViewHolder {
        return ResultViewHolder(parent, R.layout.item_result,onRecyclerItemClickListener)
    }
}
abstract class BaseViewHolder<T : BaseModel>(parent: ViewGroup, @LayoutRes itemLayoutId: Int,
                                             var onRecyclerItemClickListener: OnRecyclerItemClickListener) :
        RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(itemLayoutId, parent,
                false)), View.OnClickListener {

    override fun onClick(v: View?) {
        onRecyclerItemClickListener.onItemClicked(v, adapterPosition)
    }

    abstract fun bindData(data: T)

    init {
        itemView.setOnClickListener(this)
    }
}
abstract class BaseAdapter<T : BaseModel, U : BaseViewHolder<T>>
(var items: List<T>, var onRecyclerItemClickListener: OnRecyclerItemClickListener)
    : RecyclerView.Adapter<U>() {

    override fun getItemCount(): Int {
        return items.size
    }

    override fun onBindViewHolder(holder: U, pos: Int) {
        holder.bindData(items.get(pos))
    }
}
class ResultViewHolder(parent: ViewGroup, itemLayoutId: Int, onRecyclerItemClickListener: OnRecyclerItemClickListener) : BaseViewHolder<ResultBO>(parent, itemLayoutId, onRecyclerItemClickListener) {

    override fun bindData(data: ResultBO) {
        itemView.titleTv.setText(data.questionBO.title)

        itemView.option1Tv.setText(data.questionBO.options.get(0))
        itemView.option2Tv.setText(data.questionBO.options.get(1))
        itemView.option3Tv.setText(data.questionBO.options.get(2))
        itemView.option4Tv.setText(data.questionBO.options.get(3))

        when(data.orignalAnswer()) {
            OptionType.A -> itemView.option1Tv.setBackgroundColor(Color.GREEN)
            OptionType.B -> itemView.option2Tv.setBackgroundColor(Color.GREEN)
            OptionType.C -> itemView.option3Tv.setBackgroundColor(Color.GREEN)
            OptionType.D -> itemView.option4Tv.setBackgroundColor(Color.GREEN)
        }

        if(!data.isCorrectlyAnswered()){
            when(data.selectedOption) {
                OptionType.A -> itemView.option1Tv.setBackgroundColor(Color.RED)
                OptionType.B -> itemView.option2Tv.setBackgroundColor(Color.RED)
                OptionType.C -> itemView.option3Tv.setBackgroundColor(Color.RED)
                OptionType.D -> itemView.option4Tv.setBackgroundColor(Color.RED)
            }
        }

    }
}
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView


class AddressAdapter (private val listener: (AddressData, Int,Int) -> Unit):
    RecyclerView.Adapter<AddressViewHolder>() {
var lstAddress = emptyList<AddressData>()

fun setData(items: List<AddressData>) {
    lstAddress = items
    notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddressViewHolder {
    return AddressViewHolder.create(parent)
}

override fun getItemCount(): Int {
    return lstAddress.size
}

override fun onBindViewHolder(holder: AddressViewHolder, position: Int) {
    return holder.bind(lstAddress[position], listener, position)
}

fun getData(): List<AddressData> {
    return lstAddress
}
}
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_address.view.*

class AddressViewHolder(override val containerView: View) :
    RecyclerView.ViewHolder(containerView), LayoutContainer {

companion object {
    fun create(parent: ViewGroup): AddressViewHolder {
        val view =
                LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_address, parent, false)
        return AddressViewHolder(view)
    }
}

fun bind(
        item: AddressData,
        listener: (AddressData, Int,Int) -> Unit,
        position: Int
) {
    item.address.let {
        itemView.tvAddress.text = it
    }
    itemView.tvEdit.setOnClickListener {
        listener.invoke(item, position,R.id.tvEdit)
    }
    itemView.tvDelete.setOnClickListener {
        listener.invoke(item, position,R.id.tvDelete)
    }
    itemView . tvAddress . setOnClickListener {
        listener.invoke(item, position, R.id.tvAddress)
    }
}

}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvWorkPlace"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="@dimen/letter_medium"
    android:layout_marginTop="@dimen/letter_medium"
    android:gravity="top"
    android:text="@string/work_place"
    android:textColor="@color/black_color"
    android:textSize="@dimen/letter_medium"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvDefault"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginStart="@dimen/spacing_medium"
    android:text="@string/txt_default"
    android:textColor="#002D56"
    android:textSize="@dimen/letter_small"
    app:layout_constraintBottom_toBottomOf="@+id/tvWorkPlace"
    app:layout_constraintStart_toEndOf="@+id/tvWorkPlace"
    app:layout_constraintTop_toTopOf="@+id/tvWorkPlace" />


<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvAddress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="@dimen/spacing_small"
    android:text="76 Playfair Road, #04-06, LHK2, Singapore 367996"
    android:textSize="@dimen/letter_small"
    app:layout_constraintStart_toStartOf="@+id/tvWorkPlace"
    app:layout_constraintTop_toBottomOf="@+id/tvWorkPlace" />

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="@dimen/spacing_large"
    android:text="@string/txt_edit"
    android:textAllCaps="true"
    android:textColor="@color/colorSecondary"
    android:textSize="@dimen/letter_small"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="@+id/tvAddress"
    app:layout_constraintTop_toBottomOf="@+id/tvAddress" />

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginStart="@dimen/spacing_large"
    android:text="@string/txt_delete"
    android:textAllCaps="true"
    android:textColor="@color/colorPrimaryNew"
    android:textSize="@dimen/letter_small"
    android:textStyle="bold"
    app:layout_constraintStart_toEndOf="@+id/tvEdit"
    app:layout_constraintTop_toTopOf="@+id/tvEdit" />

<View
    android:layout_width="match_parent"
    android:layout_height="0.2dp"
    android:layout_marginTop="@dimen/spacing_medium"
    android:background="@color/divider_color"
    app:layout_constraintTop_toBottomOf="@+id/tvDelete" />

</androidx.constraintlayout.widget.ConstraintLayout>
class AddressData (
    var code: Int,
    var address:String
)
    private fun setUpAdapter() {
    adapter = AddressAdapter(fun(item: AddressData, position: Int, type: Int) {
        when (type) {
            R.id.tvEdit -> {
                context.showToast(
                        "Editbtn" + position,
                        showInReleaseBuild = true
                )
            }
            R.id.tvDelete -> {
                context.showToast(
                        "Deletebtn" + position,
                        showInReleaseBuild = true
                )
            }
            R.id.tvAddress -> {
                context.showToast(
                        "addressbtn" + position,
                        showInReleaseBuild = true
                )
            }

        }


    })
    rvAddress.adapter = adapter
    rvAddress.setEmptyView(llEmptyViewMain)
    val addressData = AddressData(1, "abcd")
    val addressData1 = AddressData(2, "abceeed")


    lstData = listOf(addressData, addressData1)
    adapter.setData(lstData)
}
    <com.example.views.CustomRecyclerView
    android:id="@+id/rvAddress"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="@dimen/mediumSize"
    android:layout_marginTop="@dimen/mediumSize"
    android:layout_marginEnd="@dimen/mediumSize"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    app:list_type="list"
    tools:listitem="@layout/item_address" />

    <include layout="@layout/layout_recycler_emptyview" />