Bis*_*uit 5 android android-chips
我正在从我的 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)
}
}
})
Run Code Online (Sandbox Code Playgroud)
结果是一行中有 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"/>
Run Code Online (Sandbox Code Playgroud)
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2027 次 |
| 最近记录: |