Home > Software design >  How to access another activity's XML from recyclerView adapter?
How to access another activity's XML from recyclerView adapter?

Time:08-27

This is my adapter's onBindViewHolder function:

    override fun onBindViewHolder(holder: InventoryHolder, position: Int) {
    holder.binding.inventoryImageView.setImageResource(productList.get(position).image)
    holder.binding.productTextInventory.text = "Name: ${productList.get(position).name}"
    holder.binding.equipButton.setOnClickListener{

        if(productList.get(position).type == "Background"){
            //change main menu background photo.
            println("Background")
        }
        if(productList.get(position).type == "Character"){
            //change game character.
            println("Character")

        }


    }
    holder.binding.productTypeInventoryText.text = "Type: ${productList.get(position).type}"
}

As you can see it's a basic game inventory system. I need to change activity_main.xml's components when equipButton clicked. For example I need to change image source here:

        <ImageView
        android:id="@ id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_row="0"
        android:layout_column="0"
        android:onClick="incScore"
        app:srcCompat="@drawable/simpson" />

How can I do this?

CodePudding user response:

You could add lambda expression function parameter in your adapter like this

class YourAdapter(private val onClickEquip:(String) -> Unit)

in your onClickListener you could use the function like this

holder.binding.equipButton.setOnClickListener{

    if(productList.get(position).type == "Background"){
        //change main menu background photo.
        onClickEquip("Background")
        println("Background")
    }
    if(productList.get(position).type == "Character"){
        //change game character.
        onClickEquip("Character")
        println("Character")

    }
}

and in your activity when you create your adapter instance like this

val adapter = YourAdapter { type ->
        when(type){
            "Character" -> //Change image
            "Background" -> //Change image
        }
}

CodePudding user response:

As I understand you have an Activity which contains a view with Adapter and you want to change a view which is part of Activity's UI from that same Adapter. In order to do so you can create an interface which will be used to pass the data between both sides. Let's say your interface looks like this:

interface OnInventoryClickListener {
    fun onItemClick(type: ProductType)
}

And inside your Adapter you have a variable of type OnIventoryClickListener which the Activity will provide:

lateinit var clickListener: OnInventoryClickListener

fun setListener(listener: OnInventoryClickListener){
    clickListener = listener
}
// ... Inside your onBindViewHolder
holder.binding.equipButton.setOnClickListener{
    clickListener.onItemClick(productList.get(position).type)
}
// ...

And from your Activity just call:

// adapter is the instance of your adapter.
adapter.setListener(object: OnInventoryClickListener {
    override fun onItemClick(type: Int) {
        // Change whatever you want based on type
    }
})

And that should do the trick. I suggest you to learn more about interfaces and how you can use them.

  • Related