E
E
Evgeny Bykov2019-07-15 18:31:25
Android
Evgeny Bykov, 2019-07-15 18:31:25

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

The code

"Fragment - holder":
<?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>

Element for 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>

I have an adapter
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
    }
}

And GridView initialization in onCreate:
gridViewField = v.findViewById(R.id.gvField)
gridViewField.adapter = ImageAdapter(context)
gridViewField.numColumns = COLUMN


Help me fix what I'm missing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2019-07-17
@TequilaOne

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</LinearLayout>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question