我要从我的API中获取一个列表,该列表包含25个国家/地区的标签,例如德国,英国,法国,意大利等...我想要2行,chips
每行10个,如果我得到30个标签,下次获取时,我想有3行,每行10条,chips
等等...
到目前为止,我还没有发现任何允许我做的事情。我快速浏览了一下,Flexbox-Layout
但它似乎不符合我的需求,我现在有下面的代码,但是我正在考虑使用Recyclerview
分段
viewModel.videoSelected.observe(viewLifecycleOwner, object : Observer<VideoPage> {
override fun onChanged(videoPage: VideoPage?) {
videoPage?.tags ?: return
val chipGroup = binding.chipGroup
val chipGroupInflater = LayoutInflater.from(chipGroup.context)
val children = videoPage.tags.map { tagName ->
val chip = chipGroupInflater.inflate(R.layout.chip_video_tag, chipGroup, false) as Chip
chip.text = tagName
chip.tag = tagName
chip.setOnClickListener {
Toast.makeText(context, tagName, Toast.LENGTH_SHORT).show()
}
chip
}
for (chip in children) {
chipGroup.addView(chip)
}
}
})
结果是一行中有25个筹码。我如何才能将其分割成多行?
我也设法用一种更简单的方式做到这一点BindingAdapter
:
<androidx.recyclerview.widget.RecyclerView
app:listVideoTagChip="@{viewModel.videoSelected.tags}"
android:id="@+id/rv_video_chips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.StaggeredGridLayoutManager"
android:orientation="horizontal"
tools:listitem="@layout/chip_video_tag"/>
private const val TAG_PER_ROW = 10
@BindingAdapter("listVideoTagChip")
fun RecyclerView.bindRecyclerView(data: List<String>?) {
val adapter: VideoTagAdapter = this.adapter as VideoTagAdapter
(layoutManager as StaggeredGridLayoutManager).spanCount =
data?.size?.let {
ceil(it.toDouble().div(TAG_PER_ROW)).toInt()
} ?: 1
adapter.submitList(data)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句