Kotlin 中的 RecyclerView itemClickListener

RecyclerView itemClickListener in Kotlin

在使用 Android 3 年后,我正在用 Kotlin 编写我的第一个应用程序。 只是对如何在 Kotlin 中将 itemClickListener 与 RecyclerView 结合使用感到困惑。

我已经尝试过 trait(编辑:现在界面)方法,非常 Java-like

public class MainActivity : ActionBarActivity() {

  protected override fun onCreate(savedInstanceState: Bundle?) {

    // set content view etc go above this line

    class itemClickListener : ItemClickListener {
      override fun onItemClick(view: View, position: Int) {
        Toast.makeText(this@MainActivity, "TEST: " + position, Toast.LENGTH_SHORT).show()
      }
    }

    val adapter = DrawerAdapter(itemClickListener())
    mRecyclerView.setAdapter(adapter)
 }

  trait ItemClickListener {
    fun onItemClick(view: View, position: Int)
  }
}

这似乎很多余所以我尝试了内部 class 方法:

inner class ItemClickListener {
    fun onItemClick(view: View, position: Int) {
        startActivityFromFragmentForResult<SelectExerciseActivity>(SELECT_EXERCISES)
    }
}

然后像这样设置适配器的点击监听器:

val adapter = WorkoutsAdapter(ItemClickListener())

但我对此仍然不满意,因为我认为可能有更好、更简洁的方法。我试图从根本上实现这样的目标: RecyclerView onClick

有什么建议吗?

最终采用了已批准答案的变体

定义函数在activity:

val itemOnClick: (View, Int, Int) -> Unit = { view, position, type ->
    Log.d(TAG, "test")
}

像这样将函数本身传递给适配器:

class ExercisesAdapter(val itemClickListener: (View, Int, Int) -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
      // other stuff up here
      val vhExercise = ExerciseVH(view) // view holder
      // on to the view holder through the extension function
      vhExercise.onClick(itemClickListener)
    }
}

下面批准的答案中循环的扩展功能。

fun <T : RecyclerView.ViewHolder> T.onClick(event: (view: View, position: Int, type: Int) -> Unit): T {
    itemView.setOnClickListener {
        event.invoke(it, getAdapterPosition(), getItemViewType())
    }
    return this
}

您可以尝试类似的方法:

public class MainActivity : ActionBarActivity() {
    protected override fun onCreate(savedInstanceState: Bundle?) {
        [...]
        val adapter = DrawAdapter(::onItemClick)
        [...]
    }
}

fun onItemClick(view: View, position: Int) {
    //Do work
}

和 SAM 转换就像在 Java 8 中一样工作,所以只需使用 lambda:

public class MainActivity : ActionBarActivity() {
    protected override fun onCreate(savedInstanceState: Bundle?) {
        [...]
        val adapter = DrawAdapter({view, position -> /*Do work*/ })
        [...]
    }
}

我有一些不同的方法。您可以为您的 ViewHolder 创建一个扩展

fun <T : RecyclerView.ViewHolder> T.listen(event: (position: Int, type: Int) -> Unit): T {
    itemView.setOnClickListener {
        event.invoke(getAdapterPosition(), getItemViewType())
    }
    return this
}

然后像这样在适配器中使用它

class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    val items: MutableList<String> = arrayListOf()

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder? {
        val inflater = LayoutInflater.from(parent!!.getContext())
        val view = inflater.inflate(R.layout.item_view, parent, false)
        return MyViewHolder(view).listen { pos, type ->
            val item = items.get(pos)
            //TODO do other stuff here
        }
    }

    override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {

    }

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


    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    }
}

我正在与我的同事一起 library 提供此类扩展。

如果有人正在寻找更 简洁 的答案,我尝试了以下 - 这与 AfzalivE[=56 的解决方案非常相似=]:

在我的适配器 class 中,我将 clickListener 作为参数传递。在 onBindViewHolder,我使用 setOnClickListener 调用 clickListener 并处理点击事件。

MyAdapter.kt:

class MyAdapter constructor(objects: ArrayList<MyObject>, val clickListener: (MyObject) -> Unit) : RecyclerView.Adapter<MyAdapter.Holder>() {

    private var mObjects : ArrayList<MyObject> = ArrayList<MyObject>()

    init {
        mObjects = objects
    }

    override fun onBindViewHolder(holder: Holder?, position: Int) {
        var item : MyObject = objects[position]

        // Calling the clickListener sent by the constructor
        holder?.containerView?.setOnClickListener { clickListener(item) }
    }

    // More code (ViewHolder definitions and stuff)...

}

注意:我需要来自列表项容器(根视图)的引用,在本例中是 containerView

然后我将我的对象作为参数传递,而不需要再次在列表中搜索它并直接在我的 Activity class 上处理它,在我设置适配器的那一刻:

MyActivity.kt:

myRecyclerView?.adapter = MyAdapter(mObjects) {
    Log.e("Activity", "Clicked on item ${it.itemName}")
}  

更新

如果需要获取点击项的位置,在回调中定义为参数,稍后传回即可。注意下面的 val clickListener: (MyObject, Int) -> Unit

MyAdapter.kt

class MyAdapter constructor(objects: ArrayList<MyObject>, val clickListener: (MyObject, Int) -> Unit) : RecyclerView.Adapter<MyAdapter.Holder>() {
    // Rest of the code...

然后在onBindViewHolder()调用回调方法时传递仓位:

override fun onBindViewHolder(holder: Holder?, position: Int) {
    var item : MyObject = objects[position]

    // Calling the clickListener sent by the constructor
    holder?.containerView?.setOnClickListener { clickListener(item, position) }
}

而在 MyActivity.kt 上,您必须更改设置适配器的方式才能获得位置。像这样:

myRecyclerView?.adapter = MyAdapter(mObjects) { itemDto: MyObject, position: Int ->
        Log.e("MyActivity", "Clicked on item  ${itemDto.someItemPropertyLikeName} at position $position")
    }

在 RecyclerView 中,您可以在 ViewHolder class 中点击膨胀视图,然后从 onBindViewHolder 回调方法中调用它,例如:

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    val view = view
    val tv_message = view.tv_message
    val tv_address = view.tv_address

    fun bind(listViewItem: ListViewItem) {
        view.setOnClickListener(View.OnClickListener {

            Toast.makeText(
                view.context, 
                "Name: " + listViewItem.name + "/n Address: " + listViewItem.address, 
                Toast.LENGTH_LONG).show()
            })
        }
    }
}

您可以从适配器调用 onBindViewHolder() 方法:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    val listViewItem: ListViewItem = mListViewItems[position]
    holder.tv_message.text = listViewItem.name
    holder.tv_address.text = listViewItem.address
    holder.bind(mListViewItems[position]);
}

2019 年 5 月更新

我认为最优雅的解决方案是将此职责交给recyclerView,而不是查看甚至适配它。

为此我们需要:

1: 创建 RecyclerItemClickListener 文件

class RecyclerItemClickListener(
        private val mRecycler: RecyclerView,
        private val clickListener: ((position: Int, view: View) -> Unit)? = null,
        private val longClickListener: ((position: Int, view: View) -> Unit)? = null
) : RecyclerView.OnChildAttachStateChangeListener {

    override fun onChildViewDetachedFromWindow(view: View) {
        view.setOnClickListener(null)
        view.setOnLongClickListener(null)
    }

    override fun onChildViewAttachedToWindow(view: View) {
        view.setOnClickListener { v -> setOnChildAttachedToWindow(v) }
    }

    private fun setOnChildAttachedToWindow(v: View?) {
        if (v != null) {
            val position = mRecycler.getChildLayoutPosition(v)
            if (position >= 0) {
                clickListener?.invoke(position, v)
                longClickListener?.invoke(position, v)
            }
        }
    }
}

2:Create/Add RecyclerView 扩展:

import android.support.v7.widget.RecyclerView
import com.app.manager.internal.common.RecyclerItemClickListener
                    
@JvmOverloads
fun RecyclerView.affectOnItemClicks(onClick: ((position: Int, view: View) -> Unit)? = null, onLongClick: ((position: Int, view: View) -> Unit)? = null) {
    this.addOnChildAttachStateChangeListener(RecyclerItemClickListener(this, onClick, onLongClick))
}

3:最后是使用(我想你用的是 kotlinx)

import kotlinx.android.synthetic.main.{your_layout_name}.*

class FragmentName : Fragment() {

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        recycler.affectOnItemClick { position, view -> /*todo*/ }
    }
}

适配器构造函数声明

class YourAdapter(private val mListener: (ItemObject) -> Unit) : RecyclerView.Adapter<ViewHolder>()

适配器::onBindViewHolder

holder.itemView.setOnClickListener {
    mListener.invoke(item) // <- item instance of ItemObject
}

使用方法

mTypesWasteAdapter = YourAdapter({ it.something()})

基本上,您将在 lambda 参数中收到作为 it 的 ItemObject。

我的解决方案就像是之前的解决方案与来自 activity 的超级干净调用的组合。

联系人适配器:

class ContactAdapter @Inject constructor() : RecyclerView.Adapter<ContactAdapter.ViewHolder>() {

    var onItemClick: ((Contact) -> Unit)? = null
    var contacts: List<Contact> = emptyList()

    ...

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val contact = contacts[position]

        holder.email.text = contact.email
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val email: TextView = itemView.email

        init {
            itemView.setOnClickListener {
                onItemClick?.invoke(contacts[adapterPosition])
            }
        }
    }
}

联系活动:

override fun setupRecyclerAdapter() {
    recyclerView.adapter = contactAdapter
    recyclerView.layoutManager = LinearLayoutManager(this)

    contactAdapter.onItemClick = { contact ->

        // do something with your item
        Log.d("TAG", contact.email)
    }
}

您可以通过两种方式访问​​ kotlin 中的 recyclerview,首先您可以直接在适配器中声明 OnClickListener 并在其中进行重定向,第二种方式是您可以将您的 onclick 重定向到您设置适配器的 fragment/activity回收商

1.

 class CartAdapter(context: Context) : RecyclerView.Adapter<CartAdapter.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_cart, parent, false));
        }

        override fun getItemCount(): Int {
            return 10;
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
           holder.itemView.rtbProductRating.setOnClickListener{

            var iNavigation= Intent(context,MainActivity::class.java)
            iNavigation.flags= Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
            context.startActivity(iNavigation)

// directly redirect your activity from adapter
           }

        }

        class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)
    } 

第二种方法是将您的适配器点击重定向到 fragment/activity,然后从那里重定向您的 activity,而不是从适配器

重定向
 class CartAdapter(context: Context, onClickListener: View.OnClickListener) : RecyclerView.Adapter<CartAdapter.ViewHolder>() {
        var context = context
        var onClickListener = onClickListener
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_cart, parent, false));
        }

        override fun getItemCount(): Int {
            return 10;
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {

//set your position to the view
            holder.itemView.rtbProductRating.tag = position

//redirect click to the fragment
            holder.itemView.rtbProductRating.setOnClickListener {
                onClickListener.onClick(holder.itemView.rtbProductRating)

            }
    //        holder.itemView.tv_waybill_count.text = holder.itemView.context.getString(R.string.waybills,5)
        }

        class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)
    }


Your fragment will look like:

class CartFragment: BaseFragment(),View.OnClickListener {
    override val layout= R.layout.frg_cart

     override fun onClick(v: View?) {
      var position=v?.tag as Int

        if(position==0){
            var iNavigation= Intent(this,MainActivity::class.java)
            iNavigation.flag=Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
            startActivity(iNavigation)
        }
    }

  override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        listener()
    }

    private fun listener() {
        cart_rv.adapter=CartAdapter(activity,this)
    }
}

在onBindViewHolder fun上添加ClickListener代码

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.vieww.textView.setText(arr.get(position))

    holder.vieww.setOnClickListener {(holder.vieww.textView.setTextColor(Color.GREEN))} // click event
}

抱歉耽搁了,从 this link 得到了一个很棒的答案,它在 Java.. 做了一些功课并将其转换为 Kotlin..

现在可以正常工作了。这是代码,

创建一个 class 名为 RecyclerItemClickListenr,

class RecyclerItemClickListenr(context: Context, recyclerView: RecyclerView, private val mListener: OnItemClickListener?) : RecyclerView.OnItemTouchListener {

private val mGestureDetector: GestureDetector

interface OnItemClickListener {
    fun onItemClick(view: View, position: Int)

    fun onItemLongClick(view: View?, position: Int)
}

init {

    mGestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
        override fun onSingleTapUp(e: MotionEvent): Boolean {
            return true
        }

        override fun onLongPress(e: MotionEvent) {
            val childView = recyclerView.findChildViewUnder(e.x, e.y)

            if (childView != null && mListener != null) {
                mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView))
            }
        }
    })
}

override fun onInterceptTouchEvent(view: RecyclerView, e: MotionEvent): Boolean {
    val childView = view.findChildViewUnder(e.x, e.y)

    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView))
    }

    return false
}

override fun onTouchEvent(view: RecyclerView, motionEvent: MotionEvent) {}

override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}}

并从 Activity/Fragment 作为

访问它
recyclerView.addOnItemTouchListener(RecyclerItemClickListenr(this, recyclerView, object : RecyclerItemClickListenr.OnItemClickListener {

        override fun onItemClick(view: View, position: Int) {
            //do your work here..
        }
        override fun onItemLongClick(view: View?, position: Int) {
            TODO("do nothing")
        }
    }))

RecyclerItemClickListener

package com.mypackage.custom 
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View


@Suppress("DEPRECATION")
class RecyclerItemClickListener(context: Context, private val mListener: OnItemClickListener?) : RecyclerView.OnItemTouchListener {

    private var mGestureDetector: GestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
        override fun onSingleTapUp(e: MotionEvent): Boolean {
            return true
        }
    })

    interface OnItemClickListener {
        fun onItemClick(view: View, position: Int)
    }

    override fun onInterceptTouchEvent(view: RecyclerView, e: MotionEvent): Boolean {
        val childView = view.findChildViewUnder(e.x, e.y)
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildPosition(childView))
            return true
        }
        return false
    }

    override fun onTouchEvent(view: RecyclerView, motionEvent: MotionEvent) {}

    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
}

对于Activity:

 recyclerView!!.addOnItemTouchListener(
                RecyclerItemClickListener(this!!, object : RecyclerItemClickListener.OnItemClickListener {
                    override fun onItemClick(view: View, position: Int) {
                        //Write your code here
                    }
                })

片段:

recyclerView!!.addOnItemTouchListener(
                RecyclerItemClickListener(this!!.activity!!, object : RecyclerItemClickListener.OnItemClickListener {
                    override fun onItemClick(view: View, position: Int) {
                        //Write your code here
                    }
                })

哦,怎么会有人喜欢这个 我们都将编辑和垃圾桶图像放在回收站视图中,并希望在单击它们时发生一些事情。这是我们的 Kotlin 示例

这是在适配器中膨胀的卡片视图

    <RelativeLayout
    android:id="@+id/editCLICK"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginStart="370dp"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:paddingTop="12dp">

    <ImageView
        android:id="@+id/ivEdit"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@color/color_Transparent"
        android:src="@drawable/ic_edit"
        android:tint="@color/color_lightBlue" />

</RelativeLayout>

然后在 Adapter 中我们做一些绑定

    override fun onBindViewHolder(holder: ParentViewHolder, position: Int) {
    val items = parentList[position]
    holder.item.text = items.dept


    holder.editCLICK.setOnClickListener {
        val i = Intent(context, EnterParentActivity::class.java)
        i.putExtra("FROM", "U")
        i.putExtra("MainActId",items.idD)
        i.putExtra("PFK",items.fkD)
        i.putExtra("ET",items.dept)
        i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(i)
    }
}


inner class ParentViewHolder(view: View):RecyclerView.ViewHolder(view){
    var item: TextView = view.findViewById(R.id.tvDept) as TextView
    var editCLICK: RelativeLayout = view.findViewById(R.id.editCLICK) as RelativeLayout
}

简单快捷可靠的享受

您不需要为 ViewHolder 或类似的东西编写扩展函数。
最佳实践;使用高阶函数

MainRecyclerAdapter

class MainRecyclerAdapter(val news: JSONArray, private val itemClickListener: (Int) -> Unit) : RecyclerView.Adapter<MainRecyclerAdapter.ViewHolder>() {}

只需添加一个高阶函数。喜欢 itemClickListener 然后转到 ViewHolder class。将此函数作为参数写入您的绑定函数并将其设置为 itemView 就像那样:

MainRecyclerAdapter.ViewHolder

 class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {

        fun bind(newsItem: JSONObject,itemClickListener:(Int)->Unit) {
            //Some Stuff here..

            itemView.setOnClickListener { itemClickListener(adapterPosition) }

        }
    }

在BindViewHolder上使用这个方法

OnBindViewHolder

 override fun onBindViewHolder(holder: MainRecyclerAdapter.ViewHolder, position: Int) {

        holder.bind(news.getJSONObject(position),itemClickListener)
    }

现在您可以在任何 activity 或片段中编写您的 onClick 函数。只需提供参数即可。

Activity 或片段

val itemOnClick: (Int) -> Unit = { position ->
                newsRecyclerView.adapter!!.notifyDataSetChanged()
                Toast.makeText(this.context,"$position. item clicked.",Toast.LENGTH_SHORT).show()
            }
 newsRecyclerView.adapter = MainRecyclerAdapter(news,itemClickListener = itemOnClick)

这是我的 MainActivity.kt class,它使用 recyclerview 填充位置数据。它有一个简单的项目点击侦听器界面,您可以实现。

    class MainActivity : AppCompatActivity() {

        private lateinit var recyclerView: RecyclerView
        private lateinit var viewAdapter: RecyclerView.Adapter<*>
        private lateinit var viewManager: RecyclerView.LayoutManager
        private var locationArrayList = arrayListOf<Location>()

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

            //create locations
            var ny = Location("New York")
            var la = Location("Los Angeles")
            locationArrayList.addAll(listOf(ny, la))

            viewManager = LinearLayoutManager(this)
            viewAdapter = LocationsAdapter(locationArrayList)

            recyclerView = findViewById<RecyclerView>(R.id.recyclerView).apply {
                // use this setting to improve performance if you know that changes
                // in content do not change the layout size of the RecyclerView
                setHasFixedSize(true)

                // use a linear layout manager
                layoutManager = viewManager

                // specify an viewAdapter 
                adapter = viewAdapter

            }

    //recycler view click listener implement
            recyclerView.addOnItemClickListener(object: OnItemClickListener {
                override fun onItemClicked(position: Int, view: View) {
                    // Your logic
                    Toast.makeText(this@MainActivity, locationArrayList[position].locationName, Toast.LENGTH_SHORT).show()
                }
            })

        }

    //on item click interface
        interface OnItemClickListener {
            fun onItemClicked(position: Int, view: View)
        }

        fun RecyclerView.addOnItemClickListener(onClickListener: OnItemClickListener) {
            this.addOnChildAttachStateChangeListener(object: RecyclerView.OnChildAttachStateChangeListener {
                override fun onChildViewDetachedFromWindow(view: View?) {
                    view?.setOnClickListener(null)
                }

                override fun onChildViewAttachedToWindow(view: View?) {
                    view?.setOnClickListener({
                        val holder = getChildViewHolder(view)
                        onClickListener.onItemClicked(holder.adapterPosition, view)
                    })
                }
            })
        }
//end of interface
    }

If someone interested in the old way implementation..

我发布了完整的示例,它也减少了您的适配器代码。它使用获取回调的旧模式..

项目级别gradle

buildscript {
    ext.kotlin_version = '1.3.10'
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"//newly added

        classpath 'com.google.gms:google-services:4.1.0' // google-services plugin

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用级别Gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'org.jetbrains.kotlin.android.extensions'//it is used for @Percelize

android {
    compileSdkVersion 28
    dataBinding {
        enabled = true
    }
    androidExtensions {
        experimental = true
    }
    defaultConfig {
        applicationId 'broadpeak.firebase.learning'
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

/*kapt {
    generateStubs = true
}*/
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-core:16.0.5'
    implementation 'com.google.firebase:firebase-firestore:17.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
    implementation 'com.google.code.gson:gson:2.8.5'

    implementation 'com.firebaseui:firebase-ui-auth:4.1.0'

    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
    ////kapt "com.android.databinding:compiler:$android_plugin_version"\ // not required above 3.2.0
    ///kapt "com.android.databinding:compiler:3.1.4"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

SubjectListActivity.class

class SubjectListActivity : BaseActivity() {

    var subjects = mutableListOf<SubjectBO>()

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

        recycler_view.itemAnimator = DefaultItemAnimator()
        recycler_view.setHasFixedSize(true)
        recycler_view.layoutManager = LinearLayoutManager(this@SubjectListActivity)

        db.collection("tagCollection").get().addOnSuccessListener { querySnapshot ->
            if (querySnapshot.isEmpty()) {
                Log.d(TAG, "onSuccess: LIST EMPTY")
            } else {
                // Convert the whole Query Snapshot to a list
                // of objects directly! No need to fetch each document.
                subjects = querySnapshot.toObjects(SubjectBO::class.java)

                if(subjects.size > 0){
                    recycler_view.adapter = SubjectAdapter(subjects, 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)
                        }
                    });
                }

            }
        }.addOnFailureListener { exception ->
            exception.printStackTrace()
        }
    }

SubjectAdapter.class

class SubjectAdapter(items: List<SubjectBO>, onRecyclerItemClickListener: OnRecyclerItemClickListener)
    : BaseAdapter<SubjectBO, SubjectViewHolder>(items, onRecyclerItemClickListener) {

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): SubjectViewHolder {
        return SubjectViewHolder(parent, R.layout.item_subject, onRecyclerItemClickListener)
    }
}

SubjectViewHolder.class

class SubjectViewHolder(parent: ViewGroup, itemLayoutId: Int, onRecyclerItemClickListener:
    OnRecyclerItemClickListener) : BaseViewHolder<SubjectBO>(parent, itemLayoutId, onRecyclerItemClickListener) {

    override fun bindData(data: SubjectBO) {
        itemView.tvTitle.setText(data.tagName)
    }
}

BaseAdapter.class

abstract class BaseAdapter<T, 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))
    }
}

BaseViewHolder.class

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)
    }
}

OnRecyclerItemClickListener.class

interface OnRecyclerItemClickListener{
    fun onItemClicked(view: View?, position: Int)
}

最后,这是一个很好的工作解决方案:

MyRecyclerAdapter.kt

class MyRecyclerAdapter(val context: Context, val items : ArrayList<Item>, val clickListener: (Int) -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.my_item, parent, false))
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as MyViewHolder).clickableView.setOnClickListener {
            clickListener(position)
        }
    }
}

class MyViewHolder (view: View) : RecyclerView.ViewHolder(view) {
    val clickableView = view.clickable_view
}

MainActivity.kt

fun appClickListener(position: Int) {
    // You got the position of ArrayList
}

my_recyclerview.adapter = MyRecyclerAdapter(this, myList, clickListener = {
    appClickListener(it)
})

略有不同,基于 denwehrle

要在片段上使用,在 OnCreateView 中

 adapter.onItemClick = {it ->
    //do something
 }

在适配器中添加 class:

var onItemClick: ((Contact)->Unit) ?= null
...

inner class contactViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val myItemView: TextView = itemView.findViewById(R.id.textView)

        init{
            itemView.setOnClickListener {
                onItemClick?.invoke(contact[adapterPosition])
            }
        }
}

您可以使用接口轻松实现此目的

class ExercisesAdapter constructor(val mItemClickListener:ItemClickListener) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    interface ItemClickListener{
        fun onItemClick(position: Int)
        fun onLongClick(position: Int)
    }

    inner class MyViewHolder(view:View): RecyclerView.ViewHolder(view){

        init {
            view.setOnClickListener{
                mItemClickListener.onItemClick(adapterPosition)
            }
            view.setOnLongClickListener{
                mItemClickListener.onLongClick(adapterPosition)
                return@setOnLongClickListener true
            }
        }
    }
}

来自您的 MainActivity

public class MainActivity : ActionBarActivity(), ExercisesAdapter.ItemClickListener {

   protected override fun onCreate(savedInstanceState: Bundle?) {

    // set content view etc go above this line
    mAdapter = ExercisesAdapter(this)
   }

   override fun onItemClick(position: Int) {
        Toast.makeText(this@MainActivity, "TEST: " + position, Toast.LENGTH_SHORT).show()
    }

    override fun onLongClick(position: Int) {
        //do long click here
    }
}

//Step 1 做一个类似

的界面
interface RecyclerViewClickListener {
    fun onItemClick(position: String)
    fun onLongClick(position: Int)
}

第 2 步内部适配器 class 像

一样将一个参数作为接口传递
class ModelAdapter(var item_list: ArrayList<UploadDocument>,var mItemClickListener:RecyclerViewClickListener) : RecyclerView.Adapter<ModelAdapter.ViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ModelAdapter.ViewHolder {
    // create a new view

    val view = LayoutInflater.from(parent.context).inflate(R.layout.upload_document_row_item, null)

    // create ViewHolder

    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ModelAdapter.ViewHolder, position: Int) {

    holder.txtRegistrationDoc?.setText(item_list[position].getdocName())
    holder.txtCertificate?.setText(item_list[position].getcertificateName())
    holder.txtFileSize?.setText(item_list[position].getfileSize())
    holder.txtCreatedOn?.setText(item_list[position].getcreatedOn())
    holder.txtModifiedOn?.setText(item_list[position].getModifiedDate())

    //holder.chkSelected.isChecked = item_list[position].isSelected()

    holder.chkSelected.tag = item_list[position].getdocName()


        holder. chkSelected!!.setOnCheckedChangeListener { buttonView, isChecked ->

            if(isChecked)
            {
                System.out.println("position>>>"+buttonView.tag.toString())
                mItemClickListener.onItemClick(buttonView.tag.toString())
            }

        }

    //(context as UploadDocumentActivity::class.java).onClickCalled("your argument here")

   /* holder.btn_delete.setOnClickListener(object : View.OnClickListener() {
        override fun onClick(v: View) {

            deleteItemFromList(v, position)


        }
    })*/

}

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


/*// confirmation dialog box to delete an unit
private fun deleteItemFromList(v: View, position: Int) {

    val builder = AlertDialog.Builder(v.getContext())

    //builder.setTitle("Dlete ");
    builder.setMessage("Delete Item ?")
        .setCancelable(false)
        .setPositiveButton("CONFIRM",
            DialogInterface.OnClickListener { dialog, id ->
                item_list.remove(position)
                notifyDataSetChanged()
            })
        .setNegativeButton("CANCEL", DialogInterface.OnClickListener { dialog, id -> })

    builder.show()

}*/


class ViewHolder(
    itemLayoutView: View) : RecyclerView.ViewHolder(itemLayoutView) {

    var item_name: TextView
    var txtRegistrationDoc: TextViewNormal?=null
    var txtCertificate: TextViewNormal?=null
    var txtFileSize: TextViewNormal?=null
    var txtCreatedOn: TextViewNormal?=null
    var txtModifiedOn: TextViewNormal?=null
    var chkSelected: CheckBox


    init {

        item_name = itemLayoutView.findViewById(R.id.txt_Name)
        txtRegistrationDoc = itemLayoutView.findViewById(R.id.txtRegistrationDoc)
        txtCertificate = itemLayoutView.findViewById(R.id.txtCertificate)
        txtFileSize = itemLayoutView.findViewById(R.id.txtFileSize)
        txtCreatedOn = itemLayoutView.findViewById(R.id.txtCreatedOn)
        txtModifiedOn = itemLayoutView.findViewById(R.id.txtModifiedOn)
        //btn_delete = itemLayoutView.findViewById(R.id.btn_delete_unit)
        chkSelected = itemLayoutView.findViewById(R.id.chk_selected)


    }



}
}

//步骤 3 在你的 activity/ Frgament

recyclerView?.adapter = ModelAdapter(documentList,object : `in`.mobilepedia.com.gicgwaliarincubationcentre.RecyclerViewClickListener
        {
            override fun onItemClick(position: String) {

            System.out.println("Position>>>>>"+position)
        }

        override fun onLongClick(position: Int) {

        }

    })

我使用高阶函数和 let 作用域函数的简单解决方案仅在 itemAction 已设置时设置侦听器

// Adapter
private var itemAction: ((Item) -> Unit)? = null

fun setItemAction(action: (Item) -> Unit) {
    this.itemAction = action
}

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    fun setItem(item: Item) {

        // ...

        itemAction?.let {
            itemView.setOnClickListener { it(item) }
        }
    }
}

并在 activity/fragment

adapter.setItemAction { // <- it is Item
  // do something with it
}

我想出了这个解决方案,在单击行时使用伴随对象和界面打开 activity。 activity 是从 main activity 打开的,因为我必须在离开前保存列表状态。

适配器

class MyAdapter(
    val dataList: List<objects.ListObject>, val listener: ItemClickListener
) : RecyclerView.Adapter<MyAdapter.ListViewHolder>()
{

    companion object {
        var mClickListener: ItemClickListener? = null
    }

    interface ItemClickListener
    {
        fun clickRow(position: Int)
    }

    override fun onBindViewHolder(holder: MyAdapter.ListViewHolder, position: Int)
    {
        holder.bindData(
            ...
        )

        mClickListener = listener
        holder.itemView.setOnClickListener { view ->

            mClickListener?.clickRow(position)
        }
    }

    ... 
}

主要activity

val context = this
private lateinit var mMyAdapter: MyAdapter

fun initList()
{
    mMyAdapter =
        MyAdapter(dataList, object : MyAdapter.ItemClickListener
        {
            override fun clickRow(position: Int)
            {
                openActivityListItems(position)
            }
        }
    )
}

fun openActivityListItems(position : Int)
{
    recyclerViewState = mListView.getLayoutManager()?.onSaveInstanceState()

    val intent = Intent(context, ListItems::class.java)
    intent.putExtra("Parameter1", dataList[position].Parameter1)
    intent.putExtra("Parameter2", dataList[position].Parameter2)
    context.startActivity(intent)
}

Kotlin

像这样制作你的适配器构造函数

    class ViewAdapter(
        private val context: Context,
        private val mListener: (DataClass) -> Unit
    ) :
        RecyclerView.Adapter<WeekRecyclerViewAdapter.ViewHolder>() {

// Your adapter code goes here

}

在您的 onBindViewHolder 中,

holder.binding.parentLayout.setOnClickListener {
                mListener.invoke(items[position]) // <- item instance of ItemObject
        }

在您的 Fragment 中,像下面那样实现

class YourFragment : Fragment(), (DataClass) -> Unit {
    override fun invoke(p1: DataClass) {

        //You will get the selected item here

    }

3 个简单步骤:

1.按如下方式传入适配器的参数:

class ListAdapter(private val mListener: (ListItemDataClass) -> Unit)

2。在 onBindViewHolder 函数中,像这样使用

override fun onBindViewHolder(holder: YourViewHolder, position: Int) {

    val item = getItem(position)

    holder.itemView.setOnClickListener {
            item?.let { it1 -> mListener.invoke(it1) }
    }
}

3。在你的activity中,像这样使用

val adapter = ListAdapter {
        Toast.makeText(this, it.title, Toast.LENGTH_SHORT).show()
}

这是一个不使用接口的简单方法,只需在您的适配器中在 viewholder 中创建一个初始化块 class.Like this

 class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    init {
        itemView.setOnClickListener{
    //your code here---
      }
    }

}