简体   繁体   English

如何在 RecyclerView 中获取项目的位置

[英]How to get position of the item in RecyclerView

I'm new in Android dev.我是 Android 开发新手。 In my code i don't using onClick method, but i using setOnClickListener and Callback.在我的代码中,我不使用 onClick 方法,但我使用 setOnClickListener 和回调。 The main problem that is in this way i don't know how to get the position of the item in RecyclerView.这种方式的主要问题是我不知道如何在 RecyclerView 中获取项目的位置。

Here is my Adapter:这是我的适配器:

class TestAdapter(val test : ArrayList<Test>, private val testAdapterCallback: (Test, Int)->Unit) : RecyclerView.Adapter<TestAdapter.ViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        val v = LayoutInflater.from(parent.context).inflate(R.layout.test_view_item, parent, false)


        return ViewHolder(v)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val num : Test = test[position]
        holder.textView.text = num.id.toString()
        holder.cardView.setTag(position)
        holder.cardView.setOnClickListener(){
            testAdapterCallback(num)
        }
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

        val cardView = itemView.findViewById<CardView>(R.id.testCardView)
        val textView = itemView.findViewById<TextView>(R.id.testTextView)

    }

}

I have added second parametr into callback but i don't know how i must change my adapter's inicializations in this peace of code:我已将第二个参数添加到回调中,但我不知道如何在此代码和平中更改适配器的初始化:

val adapter = TestAdapter(list) { item ->
                testAdapterItemClick(item)
            }

In activity i'm using this method :在活动中,我正在使用这种方法:

private fun testAdapterItemClick(item: Test) {}

Please, help me to check the position of the choosen element.请帮我检查所选元素的位置。 I need it later.我以后需要它。 Thanks in advance)提前致谢)

PS Sorry for my English PS对不起我的英语

Add the position as parameter in the callback.在回调中添加位置作为参数。

So, instead of: private val testAdapterCallback: (Test)->Unit所以,而不是: private val testAdapterCallback: (Test)->Unit

Use: private val testAdapterCallback: (Test, Int)->Unit .使用: private val testAdapterCallback: (Test, Int)->Unit

This way you can pass the position in the callback.这样你就可以在回调中传递位置。

holder.cardView.setOnClickListener(){
    testAdapterCallback(num, position)
}

In your activity:在您的活动中:

val adapter = TestAdapter(list) { item, position ->
                testAdapterItemClick(item)
            }

Create interface for your OnClickListener为您的OnClickListener创建接口

interface OnClickListener{
    fun clickItem(test: Test, index: Int)
}

Pass listener to your adapter like below.将侦听器传递给您的适配器,如下所示。

    class TestAdapter(
        var test : ArrayList<Test>?,
        val clickListener: OnClickListener,
        var mActivity: Activity
    ) :
        RecyclerView.Adapter<TestAdapter.MyViewHolder>() {
}

Now in your onBindViewHolder add click listener.现在在你的onBindViewHolder添加点击监听器。

var mtest= test !!.get(i)
    holder.cardView.setOnClickListener {
                clickListener.clickItem(mtest, i)
            }

Implement Listener to your activity and initialize it.为您的活动实施侦听器并对其进行初始化。

 this.mOnClickListener = this

Pass listener to your adapter where you passing the arraylist.将侦听器传递给您传递数组列表的适配器。

 mTestAdapter = TestAdapter(arrayList, mOnClickListener ,mActivity)

You'll get the position in your activity override method.您将在您的活动覆盖方法中获得位置。

override fun editItem(mTest: Test, index: Int) {
        if (mTest!= null) {

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM