"Фрагмент - держатель":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gvField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp">
</GridView>
</LinearLayout>
Элемент для GridView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorPrimaryDark">
<ImageView
android:id="@+id/imgField"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Имею адаптер:
class ImageAdapter(val context: Context?) : BaseAdapter() {
//Тут несколько строк инициализации массива(R.drawable.*), в примере - удалил
val gameFieldImagedList = arrayOf()
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var grid: View
if (convertView == null) {
grid = View(context)
val inflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
grid = inflater.inflate(R.layout.field_item, parent, false)
} else {
grid = convertView
}
val imageView = grid.findViewById<ImageView>(R.id.imgField)
imageView.setImageResource(gameFieldImagedList[position])
return grid
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItem(position: Int): Any {
return gameFieldImagedList[position]
}
override fun getCount(): Int {
return gameFieldImagedList.size
}
}
И инициализация GridView в onCreate:
gridViewField = v.findViewById(R.id.gvField)
gridViewField.adapter = ImageAdapter(context)
gridViewField.numColumns = COLUMN