我正在使用 groupie 作为我的 recyclerview 的适配器。[追星适配器][1]
我在其中有一个水平回收器视图,为此我使用了可绑定的项目视图。
class WidgetCarousel(private val adapter: GroupieAdapter) : BindableItem<ItemCarouselBinding>() {
override fun initializeViewBinding(view: View): ItemCarouselBinding =
ItemCarouselBinding.bind(view)
override fun createViewHolder(itemView: View): GroupieViewHolder<ItemCarouselBinding> =
super.createViewHolder(itemView).also {
it.binding.recyclerView.apply {
layoutManager =
LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
}
}
override fun bind(viewBinding: ItemCarouselBinding, position: Int) {
viewBinding.recyclerView.adapter = adapter
}
override fun getLayout(): Int = R.layout.item_carousel
}
这是我的水平 recyclerview 项目
class WidgetStoryItem(val widgetData: WidgetData?) :
BindableItem<ItemStoryBinding?>() {
var selectedWidgetId: String? = null
override fun getLayout(): Int = R.layout.item_story
override fun initializeViewBinding(view: View): ItemStoryBinding =
ItemStoryBinding.bind(view)
override fun bind(viewBinding: ItemStoryBinding, position: Int) {
viewBinding.storyTitle.text = widgetData?.body?.title?.replace(" ", "\n")
viewBinding.storyImg.load(widgetData?.body?.iconUrl)
selectedWidgetId?.let {
if (it == widgetData?.id)
viewBinding.storyImgBg.background =
ContextCompat.getDrawable(
viewBinding.storyImgBg.context,
R.drawable.bg_selected_story_oval_blue
)
else
viewBinding.storyImgBg.setBackgroundResource(0)
}
}
}
我使用它如下。我有一个组项目,我将它们添加为
var storyAdapter = GroupieAdapter()
storyAdapter.addAll(storyGroup)
val widgetCarousel: WidgetCarousel = WidgetCarousel(storyAdapter)
groupAdapter.add(0, widgetCarousel)
点击的项目是
private val onItemClickListener = OnItemClickListener { item, _ ->
if (item is WidgetStoryItem) {
item.widgetData?.let {
item.selectedWidgetId=it.id
storyAdapter.notifyDataSetChanged()
}
}
我想在用户单击它时显示用户选择的视图,并将其他视图保持为未选中状态。对我来说,选择发生了,但它无法从其他项目视图中删除选择。有什么建议么?[1]:https ://github.com/lisawray/groupie