RecyclerView显示多种类型 对于这个问题我也好无语呀,这个很简单呀。 纯体力活。 RecyclerView课程地址 Android控件之RecyclerView 相关提问: Recycleview 多种条目类型 如何绑定数据 案例源码地址: https://github.com/TrillGates/RecyclerViewMultiTypeDemo 效果: 解析 网络权限
主布局activity_main.xml
主Activity:MainActivity.kt
class MainActivity : AppCompatActivity() {
private val contentAdapter by lazy {
ContentListAdapter()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//初始化数据
initData()
//初始化控件
initView()
}
private fun initView() {
contentListView.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
this.adapter = contentAdapter
addItemDecoration(object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
outRect.bottom = 2
}
})
}
}
private fun initData() {
val dataJson = requestData.resultJson
val gson = Gson()
val response = gson.fromJson
(dataJson, Response::class.java)
contentAdapter.addData(response.data)
println(response.code)
println(response.message)
println(response.data.size)
}
}
适配器ContentListAdapter.kt
class ContentListAdapter : RecyclerView.Adapter() {
private val data = ArrayList()
companion object {
const val NONE_IMG: Int = 0
const val ONE_IMG: Int = 1
const val TWO_IMG: Int = 2
const val THREE_IMG: Int = 3
}
fun addData(contents: List) {
data.clear()
data.addAll(contents)
notifyDataSetChanged()
}
class InnerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
override fun getItemViewType(position: Int): Int {
return when (data[position].covers.size) {
0 -> NONE_IMG
1 -> ONE_IMG
2 -> TWO_IMG
else -> THREE_IMG
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): InnerHolder {
val itemView: View
when (viewType) {
NONE_IMG -> {
val itemBind = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.item_no_image,
parent,
false
)
itemView = itemBind.root
itemView.tag = itemBind
}
ONE_IMG -> {
val itemBind = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.item_one_image,
parent,
false
)
itemView = itemBind.root
itemView.tag = itemBind
}
TWO_IMG -> {
val itemBind = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.item_two_image,
parent,
false
)
itemView = itemBind.root
itemView.tag = itemBind
}
else -> {
val itemBind = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.item_three_image,
parent,
false
)
itemView = itemBind.root
itemView.tag = itemBind
}
}
return InnerHolder(itemView)
}
override fun getItemCount(): Int {
return data.size
}
override fun onBindViewHolder(holder: InnerHolder, position: Int) {
val dataBinding = holder.itemView.tag
val data = data[position]
when (dataBinding) {
is ItemNoImageBinding -> {
dataBinding.item = data
}
is ItemOneImageBinding -> {
dataBinding.item = data
}
is ItemTwoImageBinding -> {
dataBinding.item = data
}
is ItemThreeImageBinding -> {
dataBinding.item = data
}
}
}
}
数据bean类 data class ListItem(
val title: String,
val covers: ArrayList,
val viewCount: Int,
val commentCount: Int
);
图片加载工具类 class ImageLoadUtils {
companion object {
@BindingAdapter("imagePath")
@JvmStatic
fun loadImage(image: ImageView, imageUrl: String?) {
if (!TextUtils.isEmpty(imageUrl)) {
Glide.with(image.context).load(imageUrl).into(image)
}
}
}
}
布局之类的代码请下载吧。 就是这么简单,全部代码请到github上载。 RecyclerViewMultiTypeDemo