【问题标题】:change background of all other elements in recyclerview更改recyclerview中所有其他元素的背景
【发布时间】:2018-09-03 11:10:37
【问题描述】:

当用户单击recyclerView 中的cardView 时,其背景颜色会改变以显示其被选中。我正在努力解决的是,当我选择另一张卡片时,我希望之前选择的卡片返回“未选择”表格。

如何让之前选择的cardview 恢复正常?

这是我目前所拥有的

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

截图:

适配器类

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 调用方法

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

谢谢

【问题讨论】:

  • 请同时发布您的适配器类
  • @Quicklearner 完成,谢谢

标签: android android-recyclerview android-viewholder


【解决方案1】:

您需要做的就是在 viewholders 的 onclick 方法中单独保存该位置并为该位置提供 notifyItemChanged。

    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
    }

然后在bindview支架上

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

【讨论】:

  • 我不确定如何以填充 recyclerview 的方式实现这一点,我正在使用 groupie
  • 尝试获取要更改颜色的 onitemclick 方法。然后将其位置保存在全局变量中。在上面给出的 onbind 视图持有者中做同样的事情。检查您的当前位置和全局保存的位置在 onbindview 中是否相同。如果它们相同,则该位置是您单击的位置,您可以更改颜色,否则部分应用所需的默认颜色。每次单击后不要忘记更新适配器。在我的情况下 notifyItemChanged(position) 是这样做的
猜你喜欢
  • 1970-01-01
  • 2021-01-18
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多