Поскольку ваши данные являются динамическими, я рекомендую вам использовать RecyclerView
с GridLayoutManager
и spanCount
из 2.
Макет вашего элемента довольно прост (просто TextView), ноВы можете использовать эту технику с более сложными макетами предметов (например, карты, изображения + ярлыки и т. д.):
There are many steps, but it's something you will use a lot, and every RV follows the same pattern:
- Layout for 1 item
- RV ViewHolder (matching item layout)
- Data Model (you can just use String)
- RV Adapter (just follow the pattern)
- Activity layout (with an RV)
- Activity onCreate (to setup the adapter)
Note: there are 3 choices for the LayoutManager
- LinearLayoutManager
- GridLayoutManager (we'll use this)
- StaggeredGridLayoutManager
1. item_language.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Width and Height must be wrap -->
<TextView
android:id="@+id/languageName"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:text="@tools:sample/cities" />
<!-- there are only a few @tools:sample choices available -->
</androidx.constraintlayout.widget.ConstraintLayout>
2. LanguageViewHolder
class LanguageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// Matches views in item_language.xml
private val languageName = itemView.languageName
fun bind(language: String) {
languageName.text = language
}
}
4. LanguagesAdapter
class LanguagesAdapter(private val data: List<String>) :
RecyclerView.Adapter<LanguageViewHolder>() {
override fun getItemCount() = data.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
LayoutInflater.from(parent.context)
.inflate(R.layout.item_language, parent, false)
.let { LanguageViewHolder(it) }
override fun onBindViewHolder(holder: LanguageViewHolder, position: Int) =
holder.bind(data[position])
}
5. activity_my.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<!-- the rest of your layout -->
<!-- set Orientation, LayoutManager, and SpanCount in your XML -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/languagesGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
tools:listitem="@layout/item_language" />
</androidx.constraintlayout.widget.ConstraintLayout>
6. MyActivity onCreate
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_grid)
val languages = listOf("English", "Chinese", "Spanish", "German", "French")
languagesGrid.adapter = LanguagesAdapter(languages)
}