RecyclerView is still writing Adapter? Abandon it forever (1)

RecyclerView is still writing Adapter? Abandon it forever (1)

I won’t say what the usage of RecyclerView looks like. Android students know that you need to write an adapter and then write a layout file in the adapter. Although there are third-party frameworks that can greatly reduce the amount of code, there are still some problems. I wonder if RecyclerView can be used directly like TextView like setText("xxx")? The answer is yes, today I will introduce you a version of Android with a framework called CHGAdapter. Here we mainly introduce the Kotlin version of Android.

Not much to say, now I will introduce the specific usage method.

To display a simple list,
first create a model: SongModel

Because we need to implement a setText method similar to TextView to use RecyclerView, we need to tell RecyclerView to have two points.

1. Which layout file is used by the data object. 2. Which ViewHolder is used by the data object. Then we need the model to implement the interface

com.chg.adapter.Model

open class SongModel : Model {

    var iconName:Int = 0
    var name:String? = null
    var songer:String? = null

    constructor(iconName: Int, name: String?, songer: String?) {
        this.iconName = iconName
        this.name = name
        this.songer = songer
    }

    override fun getResource(position: Int): Int {
        return R.layout.song_item
    }

    override fun getHolderClass(position: Int): Class<*> {
        return SongViewHolder::class.java
    }
}

Next is to write the code in our SongActivity. Put a RecyclerView in the layout file, and then the code in the activity is as follows.

class SongActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerView = findViewById(R.id.recyclerView)
        //设置数据
        recyclerView.models = getModels()
    }

    fun getModels(): List<Model> {
        var models = mutableListOf<SongModel>()
        for (index in 1..1000) {
            models.add(SongModel(R.drawable.music,"歌曲名称:$index","歌手名字:$index"))
        }
        return models
    }

}

Run it to see the effect
RecyclerView is still writing Adapter?  Abandon it forever (1)

Display multiple types of data
description: In order to make each demo look clear, each demo creates a separate Activity.

Here we create another Model named AlbumModel. code show as below

open class AlbumModel : Model {
     var name: String? = null
     var songer: String? = null

    constructor(name: String?, songer: String?) {
        this.name = name
        this.songer = songer
    }

    override fun getResource(position: Int): Int {
        return R.layout.album_item
    }

    override fun getHolderClass(position: Int): Class<*> {
        return AlbumViewHolder::class.java
    }
}

Then create our activity named RecommendActivity. code show as below

class RecommendActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerView = findViewById(R.id.recyclerView)
        //设置数据
        recyclerView.models = getModels()
    }

    fun getModels(): List<Model> {
        var models = mutableListOf<Model>()
        for (i in 0..99) {
            if (i % 2 == 0) {
                models.add(SongModel(R.drawable.music,"歌曲名称:$i","歌手名字:$i"))
            } else {
                models.add(AlbumModel("专辑名称:$i", "歌手"))
            }
        }
        return models
    }
}

Here we alternately display 2 different types of data. Therefore, when creating an array, the data is constructed by the algorithm of taking the value of 2. The operation effect is as follows

RecyclerView is still writing Adapter?  Abandon it forever (1)

You can see that we are adding the data in RecyclerView just to create a model, set the layout file and ViewHolder, put the model data into the List, and then the RecyclerView can be displayed as expected. In this way, in the future, we only need to add a new data type RecyclerView to the array to display the corresponding ItemViw according to the order of the data in the array.

Due to space issues, let's briefly introduce it here. For more introduction, you can download the demo, or follow up articles. Or join the QQ group to discuss and exchange

Android(CHGAdapter)

Java:https://github.com/chenhaigang888/CHGAdapter_android
Kotlin:https://github.com/chenhaigang888/CHGAdapter_Kotlin
Ios(CHGAdapter)

oc:https://github.com/chenhaigang888/CHGAdapter
swift:https://github.com/chenhaigang888/CHGAdapter_swift
HarmonyOS(UltimateProvider)

java: https://github.com/chenhaigang888/UltimateProvider
If you have any questions or exchanges regarding the use, please join the QQ group: 494648687

Guess you like

Origin blog.51cto.com/15073116/2582290