Answer the question
In order to leave comments, you need to log in
How to fix the area that an item in a GridView captures?
Good evening! I'm trying to work with such a thing as GridView. But when creating everything, I have every item in the GridView as tall as 2 of my screens when scrolling. I have the following
<?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>
<?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
}
}
gridViewField = v.findViewById(R.id.gvField)
gridViewField.adapter = ImageAdapter(context)
gridViewField.numColumns = COLUMN
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question