“This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The Definitive Guide to Android Programming, Chapter 19, chapter 1, is here to develop a new application. It translates to beat box. This project will learn how to use the Databinding tool in the Jetpack Architecture component library and use it to implement the new MVVM architecture. In addition, you will learn to use Assets System to store sound files.

Why MVVM architecture

Fragments and activities begin to swell and become difficult to understand and expand. It takes a long time to add new features or fix bugs. At this point, the controller layer needs to do functional separation. The MVVM architecture model helps us do this.

With respect to architectural patterns, each person’s implementation will be somewhat different due to different businesses and scenarios.

MVVM ViewModel and Jetpack ViewModel

The ViewModel in MVVM is a different concept from the ViewModel class in the Jetpack library. To avoid confusion, the two are named differently: one is called a ViewModel and the other a ViewModel. 😑

3. Create a BeatBox app

Create a new app BeatBox and add RecyclerView to the view.

<? The XML version = "1.0" encoding = "utf-8"? > <androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" />Copy the code

Implement simple data binding

This section will not call findViewById(…) Do reference views.

Start by configuring the plug-in in build.gradle to enable data binding.

plugins {
    ...
    id 'kotlin-kapt'
}

android {
    ...
    buildFeatures{
        dataBinding true
    }
}
Copy the code

Then transform the general layout into a data-bound layout.

<? The XML version = "1.0" encoding = "utf-8"? > <layout xmlns:android="http://schemas.android.com/apk/res/android"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </layout>Copy the code

The tag tells the data binding tool, “This layout should be your problem.” Upon receiving the task, the data binding tool generates a binding class. By default, the newly generated Binding class is named after the layout file with a Binding suffix. The naming format is CamelCase.

For example, activity_main.xml has an ActivityMainBinding binding class. The root attribute refers to the layout view structure, which also refers to other controls in the layout labeled android: ID.

ActivityMainBinding has two references, root and recyclerView, where the former refers to the whole layout and recyclerView refers to the same recyclerView, as shown in the following figure:

MainActivity. Kt:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)

        binding.recyclerView.apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = SoundAdapter()
        }
    }

    private inner class SoundHolder(private val binding:ListItemSoundBinding):RecyclerView.ViewHolder(binding.root){

    }

    private inner class SoundAdapter():RecyclerView.Adapter<SoundHolder>(){

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
            SoundHolder{
                val binding = DataBindingUtil.inflate<ListItemSoundBinding>(
                    layoutInflater,
                    R.layout.list_item_sound,
                    parent,
                    false
                )
                return SoundHolder(binding)
        }

        override fun onBindViewHolder(holder: SoundHolder, position: Int) {

        }
        override fun getItemCount() = 0
    }
}
Copy the code

The layout file res/layout/list_item_sound.xml

<? The XML version = "1.0" encoding = "utf-8"? > <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/apk/res-auto"> <Button android:layout_width="match_parent" android:layout_height="120dp" tools:text="Sound name" /> </layout>Copy the code

RecyclerView configuration completed, the next section began to get some data to display.

Import assets

Here the sound files will be packaged with Assets, and the asset resources can be entered into the APK package. Files can be called directly from the code. Usually used to place a large number of images and sound resources.

The AssetManager can refer to: developer.android.com/reference/a…

As shown in the picture, create a folder and put the music resources into it.

A first over, learn not to move. Divided into two, ha ha ha ~~~~~~

other

BeatBox Project Demo address:

Github.com/visiongem/A…


🌈 follow me ac~ ❤️

Public account: Ni K Ni K