Cle*_*ven 2 android android-viewholder android-recyclerview
当用户单击背景cardView中的a 时recyclerView,其背景会改变颜色以显示其所选内容。我正在努力解决的是,当我选择另一张卡时,我希望之前选择的卡返回到“未选择”形式。
如何才能让之前的选择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
}
}
Run Code Online (Sandbox Code Playgroud)
截屏:
适配器类
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
Run Code Online (Sandbox Code Playgroud)
}
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)
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 6
您要做的就是在 viewholder onclick 方法中保存该位置并单独通知该位置。
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
}
Run Code Online (Sandbox Code Playgroud)
然后在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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1370 次 |
| 最近记录: |