2

When a user clicks a cardView in the recyclerView its background changes color to show its selected. What I'm struggling with is that when I select another card I want the previously selected one to go back to "unselected" form.

How can I get the previously selected cardview to revert back to normal?

This is what I have so far

private fun updateRecyclerViewTest(items: List<Item>) {
    val list = ArrayList<Item>()
    fun init() {
        recycler_view_test.apply {
            layoutManager = LinearLayoutManager(this@EditModActivity, LinearLayoutManager.HORIZONTAL, false)
            adapter = GroupAdapter<ViewHolder>().apply {
                testSection = Section(items)
                add(testSection)
                setOnItemClickListener(onItemClick)
            }
        }
        shouldInitRecyclerView = false
    }
    if (shouldInitRecyclerView)
        init()
    else
    {
        testSection.update(items)
        testSection.setHeader(items[1])
    }
}

private val onItemClick = OnItemClickListener { item, view ->
    if (item is TestItem) {
        view.backgroundColorResource = R.color.colorAccent1
        txtview1.text = item.test.Desc
        txtview2.text = item.test.module
    }
}

Screenshot:

enter image description here

adapterclass

import android.content.Context
import com.version.crt.markbuddy.R
import com.version.crt.markbuddy.model.Test
import com.xwray.groupie.kotlinandroidextensions.Item
import com.xwray.groupie.kotlinandroidextensions.ViewHolder
import kotlinx.android.synthetic.main.item_tape.*

class TestItem(val test: Test,
           val testId: String,
           private val context: Context)
: Item() {
override fun bind(viewHolder: ViewHolder, position: Int) {
    viewHolder.textView_title.text = test.Desc
    viewHolder.textView_date.text = "Date: 2018/09/02"
    viewHolder.textView_time.text = "Time: 09:00"
    viewHolder.textView_weighting.text = "Weighting: 50%"
    viewHolder.textView_total.text = "Total: 100"
    viewHolder.textView_achieved.text = "Achieved: 50"
    viewHolder.textView_contribution.text = "End weight: 25%"
}
override fun getLayout() = R.layout.item_tape1

}

Firebase call method

fun addModInfoListener(type: String,module: String,context: Context, onListen: (List<Item>) -> Unit): ListenerRegistration {
    val items = mutableListOf<Item>()
    return firestoreInstance.collection("test").whereEqualTo("module", module).orderBy("Desc")
            .addSnapshotListener { querySnapshot, firebaseFirestoreException ->
                if (firebaseFirestoreException != null) {
                    Log.e("FIRESTORE", "Module listener error.", firebaseFirestoreException)
                    return@addSnapshotListener
                }
                querySnapshot!!.documents.forEach {
                    items.add(TestItem(it.toObject(Test::class.java)!!, it.id, context))
                }
                onListen(items)
            }
}

Thank you

Cleaven
  • 974
  • 2
  • 9
  • 30

1 Answers1

6

What you have to do is like save the position and notifyItemChanged for that position alone in the viewholders onclick method.

    int selected_position = RecyclerView.NO_POSITION;
    @Override
    public void onClick(View view) {
        if (getAdapterPosition() == RecyclerView.NO_POSITION) return;

        // Updating old as well as new positions
        notifyItemChanged(selected_position);
        selected_position = getAdapterPosition();
        notifyItemChanged(selected_position);

        // Do your another stuff for your onClick
    }

and then in on bindview holder

   @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


    if (selected_position == position) {
        // Change color for your selected item
    } else {
       // Change color for rest of the items
    }
Abhilash
  • 500
  • 4
  • 13
  • I'm not sure how to implement that in my way of populating the recylcerview, i am using groupie – Cleaven Sep 03 '18 at 11:32
  • 1
    try to get the onitemclick method where you are changing the color . Then save its position in a global variable . Do the same thing in the onbind view holder as given above. check your current position and the globally saved position are same inside onbindview. if they are same then that position is your clicked position you can change the color and in else part apply the default color needed . Dont forget to update the adapter after each click . in my case notifyItemChanged(position) does that – Abhilash Sep 03 '18 at 11:54