本文已授权微信公众号:鸿洋 在微信公众号平台原创首发。RecyclerView复杂适配器的“终极形态”?代码更解耦
前言
RecyclerView是Android开发中很常用的控件,市面上也有很多种封装,使其更易用,但是面对复杂的适配器需求,则很难做到逻辑清晰且解耦,比如聊天消息的适配器
正文
1.首先我们用最原始的方法写一个简单的聊天消息的rv
实现图如下:
代码如下:
class MainActivity : AppCompatActivity() {
//数据源: type to data
val msgData = mutableListOf<Pair<Int, Any>>(
1 to "文本消息",
2 to R.drawable.ic_launcher,//图片消息
3 to "tips消息",
1 to "文本消息2",
4 to "不支持的消息",
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//初始化rv
val rv = RecyclerView(this)
setContentView(rv)
val llm = LinearLayoutManager(this)
llm.orientation = LinearLayoutManager.VERTICAL
rv.layoutManager = llm
//适配器
rv.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
//根据不同的viewType创建不同的viewHolder
return when (viewType) {
1 -> TextVH(parent)
2 -> ImageVH(parent)
3 -> TipsVH(parent)
else -> NoMatchVH(parent)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
//判断相应type,并根据数据展示相应的view
when (getItemViewType(position)) {
1 -> {
(holder.itemView as TextView).text = msgData[position].second.toString()
}
2 -> {
(holder.itemView as ImageView).setImageResource(msgData[position].second as Int)
}
3 -> {
(holder.itemView as TextView).text = msgData[position].second.toString()
}
}
}
override fun getItemCount(): Int = msgData.size//条目的数量
override fun getItemViewType(position: Int): Int =
msgData[position].first//条目的type,使用数据源的type
}
}
//文字的vh
class TextVH(parent: ViewGroup) : RecyclerView.ViewHolder(TextView(parent.context).apply {
textSize = 22f
})
//图片的vh
class ImageVH(parent: ViewGroup) : RecyclerView.ViewHolder(ImageView(parent.context))
//提示性文字的vh
class TipsVH(parent: ViewGroup) : RecyclerView.ViewHolder(TextView(parent.context))
//没有匹配到现有type的vh,可能是新版本的消息
class NoMatchVH(parent: ViewGroup) : RecyclerView.ViewHolder(TextView(parent.context))
}
这时我们发现,设置view数据都集中在了onBindViewHolder方法中,类型多了或者逻辑多了后会造成方法非常臃肿,所以我们抽出来一个BaseViewolder出来,将设置view数据的方法抽出来,代码如下:
class MainActivity : AppCompatActivity() {
//数据源: type to data
val msgData = mutableListOf<Pair<Int, Any>>(
1 to "文本消息",
2 to R.drawable.ic_launcher,//图片消息
3 to "tips消息",
1 to "文本消息2",
4 to "不支持的消息",
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//初始化rv
val rv = RecyclerView(this)
setContentView(rv)
val llm = LinearLayoutManager(this)
llm.orientation = LinearLayoutManager.VERTICAL
rv.layoutManager = llm
//适配器
rv.adapter = object : RecyclerView.Adapter<BaseVH>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BaseVH {
//根据不同的viewType创建不同的viewHolder
return when (viewType) {
1 -> TextVH(parent)
2 -> ImageVH(parent)
3 -> TipsVH(parent)
else -> NoMatchVH(parent)
}
}
override fun onBindViewHolder(holder: BaseVH, position: Int) {
//**************调用setData方法让viewHolder根据数据展示相应的view
holder.setData(msgData[position].second)
}
override fun getItemCount(): Int = msgData.size//条目的数量
override fun getItemViewType(position: Int): Int =
msgData[position].first//条目的type,使用数据源的type
}
}
//******统一封装vh的setData方法,使其解耦
abstract class BaseVH(val parent: ViewGroup, val view: View) :
RecyclerView.ViewHolder(view) {
abstract fun setData(data: Any)
}
//文字的vh
class TextVH(parent: ViewGroup) : BaseVH(parent, TextView(parent.context).apply {
textSize = 22f
}) {
override fun setData(data: Any) {
(view as TextView).text = data as String
}
}
//图片的vh
class ImageVH(parent: ViewGroup) : BaseVH(parent, ImageView(parent.context)) {
override fun setData(data: Any) {
(view as ImageView).setImageResource(data as Int)
}
}
//提示性文字的vh
class TipsVH(parent: ViewGroup) : BaseVH(parent, TextView(parent.context)) {
override fun setData(data: Any) {
(view as TextView).text = data as String
}
}
//没有匹配到现有type的vh,可能是新版本的消息
class NoMatchVH(parent: ViewGroup) : BaseVH(parent, TextView(parent.context)) {
override fun setData(data: Any) {
(view as TextView).text = "暂不支持的消息,请更新应用"
}
}
}
但是看到上面创建viewholder的方法onCreateViewHolder,新增或修改type还是得每次自己手动修改
所以我们可以使用一个map,以type为key,并将需要创建的viewHolder的构造传进去,如下:
//装type:viewHolder构造的map
val viewHolderMap = HashMap<Int, (ViewGroup) -> BaseVH>().apply {
put(1, ::TextVH)
put(2, ::ImageVH)
put(3, ::TipsVH)
}
//并修改创建viewHolder的方法
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BaseVH {
//根据不同的viewType创建不同的viewHolder
return (viewHolderMap[viewType] ?: ::NoMatchVH).invoke(parent)
}
这样改造后,adapter中几乎就没有什么代码了,代码都移动到了各自的viewHolder中了,但是在新增viewHolder类型的时候这个viewHolderMap比较容易被忽略,能不能让他自动将所有BaseVH的子类构造和相应的type添加进去呢?
java能通过反射拿到一个类所有的子类吗?能也不能,实现起来很麻烦,而且在安卓系统上不一定行得通,但是kotlin反射可以
kotlin反射可以获取密封类(sealed class)的所有直接子类,所以我们可以改造下
将BaseVH改为sealed class,然后在初始化adapter的时候通过kotlin的反射来获取所有直接子类的构造(或class)
ps:使用kotlin的反射需要引入反射包
//kotlin反射
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
然后修改成如下代码:
class MainActivity : AppCompatActivity() {
//数据源: type to data
val msgData = mutableListOf<Pair<Int, Any>>(
1 to "文本消息",
2 to R.drawable.ic_launcher,//图片消息
3 to "tips消息",
1 to "文本消息2",
4 to "不支持的消息",
)
companion object {
//******装type:viewHolder构造的map
val viewHolderMap = HashMap<Int, Constructor<out BaseVH>>()
init {
val fl = FrameLayout(App.instance)
//******遍历class并构建出对象获取其type,并自动注册到viewHolderMap中
BaseVH::class.sealedSubclasses.forEach {
val constructor = it.java.getConstructor(ViewGroup::class.java)
val baseVH = constructor.newInstance(fl)
viewHolderMap[baseVH.type] = constructor
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//初始化rv
val rv = RecyclerView(this)
setContentView(rv)
val llm = LinearLayoutManager(this)
llm.orientation = LinearLayoutManager.VERTICAL
rv.layoutManager = llm
//适配器
rv.adapter = object : RecyclerView.Adapter<BaseVH>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BaseVH {
//**************根据不同的viewType创建不同的viewHolder
return viewHolderMap[viewType]?.newInstance(parent) ?: NoMatchVH(parent)
}
override fun onBindViewHolder(holder: BaseVH, position: Int) {
//调用setData方法让viewHolder根据数据展示相应的view
holder.setData(msgData[position].second)
}
override fun getItemCount(): Int = msgData.size//条目的数量
override fun getItemViewType(position: Int): Int =
msgData[position].first//条目的type,使用数据源的type
}
}
//统一封装vh的setData方法,使其解耦
sealed class BaseVH(val parent: ViewGroup, val view: View) :
RecyclerView.ViewHolder(view) {
abstract val type: Int
abstract fun setData(data: Any)
}
//文字的vh
class TextVH(parent: ViewGroup) : BaseVH(parent, TextView(parent.context).apply {
textSize = 22f
}) {
override val type = 1
override fun setData(data: Any) {
(view as TextView).text = data as String
}
}
//图片的vh
class ImageVH(parent: ViewGroup) : BaseVH(parent, ImageView(parent.context)) {
override val type = 2
override fun setData(data: Any) {
(view as ImageView).setImageResource(data as Int)
}
}
//提示性文字的vh
class TipsVH(parent: ViewGroup) : MainActivity.BaseVH(parent, TextView(parent.context)) {
override val type = 3
override fun setData(data: Any) {
(view as TextView).text = data as String
}
}
//没有匹配到现有type的vh,可能是新版本的消息
class NoMatchVH(parent: ViewGroup) : MainActivity.BaseVH(parent, TextView(parent.context)) {
override val type = 99999
override fun setData(data: Any) {
(view as TextView).text = "暂不支持的消息,请更新应用"
}
}
}
这样改造后,就可以直接继承BaseVH,并重写一下type,就可以自动注册了,减少大脑负担
ps:在BaseVH中加一个abstract的type,这样子类不重写type就编译不过去,但是需要统一构造的入参类型,并且有点骚操作; 如果不想这样处理的话,可以将type改为静态的,然后可以直接通过class反射获取type的值,或将type放到类名上(骚操作).但后两种方法是没有编译时提醒的,很容易忘.
结语
上面的方案就是我认为的RecyclerView复杂适配器且更解耦的终极形态.
扩展
要问还有更好的方式吗?
我觉得有!那就是使用compose,当然目前用compose的人或公司很少,但也是一种比较超前的而且我觉得是未来的终极形态,代码如下:
//数据源: type to data
val msgData = mutableListOf<Pair<Int, Any>>(
1 to "文本消息",
2 to R.drawable.ic_launcher,//图片消息
3 to "tips消息",
1 to "文本消息2",
4 to "不支持的消息",
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
//compose的竖向RecyclerView
LazyColumn(Modifier.fillMaxSize()) {
//遍历数据源,展示数据对应的view
msgData.forEach {
item {
//根据type的不同展示不同的view
(ItemFunctions.itemFunctionMap[1]
?: ItemFunctions::NoMatchItem_99999).invoke(it.second)
}
}
}
}
}
object ItemFunctions {
@Composable
fun TextItem_1(any: Any) {
Text(text = any as String, fontSize = 22.sp)
}
@Composable
fun ImgItem_2(any: Any) {
Image(painter = painterResource(id = any as Int), contentDescription = "")
}
@Composable
fun TipsItem_3(any: Any) {
Text(text = any as String)
}
@Composable
fun NoMatchItem_99999(any: Any) {
Text(text = "暂不支持的消息,请更新应用")
}
@JvmStatic
val itemFunctionMap = HashMap<Int, @Composable (Any) -> Unit>()
init {
ItemFunctions::class.functions
.filter { it.javaMethod?.declaringClass == ItemFunctions::class.java }
.forEach {
val type = it.name.split('_').lastOrNull()?.toIntOrNull() ?: return@forEach
itemFunctionMap[type] = it as @Composable (Any) -> Unit
}
}
}
ps:这里使用了骚操作,将type放在了方法名的末尾
pps:这段代码目前是运行不起来的,因为kotlin暂时还不支持引用@Composable的Function,但能运行起来也是和上面View展示的一样的效果
ppps:当然也可以在msgData.forEach{}内用when来做,但后续type都得手动添加,如下:
//数据源: type to data
val msgData = mutableListOf<Pair<Int, Any>>(
1 to "文本消息",
2 to R.drawable.ic_launcher,//图片消息
3 to "tips消息",
1 to "文本消息2",
4 to "不支持的消息",
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
//compose的竖向RecyclerView
LazyColumn(Modifier.fillMaxSize()) {
//遍历数据源,展示数据对应的view
msgData.forEach {
item {
//根据type的不同展示不同的view
when (it.first) {
1 -> TextItem(any = it.second)
2 -> ImgItem(any = it.second)
3 -> TipsItem(any = it.second)
else -> NoMatchItem()
}
}
}
}
}
}
@Composable
fun TextItem(any: Any) {
Text(text = any as String, fontSize = 22.sp)
}
@Composable
fun ImgItem(any: Any) {
Image(painter = painterResource(id = any as Int), contentDescription = "")
}
@Composable
fun TipsItem(any: Any) {
Text(text = any as String)
}
@Composable
fun NoMatchItem() {
Text(text = "暂不支持的消息,请更新应用")
}
end
对Kotlin或KMP感兴趣的同学可以进Q群 101786950
如果这篇文章对您有帮助的话
可以扫码请我喝瓶饮料或咖啡(如果对什么比较感兴趣可以在备注里写出来)