简体   繁体   中英

How to get position of the item in RecyclerView

I'm new in Android dev. In my code i don't using onClick method, but i using setOnClickListener and Callback. The main problem that is in this way i don't know how to get the position of the item in 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

Add the position as parameter in the callback.

So, instead of: private val testAdapterCallback: (Test)->Unit

Use: 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

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.

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

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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