0

Пытаюсь работать с такой штукой, как GridView. Но при создании всего, у меня каждый элемент в GridView имеет высоту как 2 моих экрана при прокрутке. Имею следующее "Фрагмент - держатель":

<?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

Помогите исправить, что я упустил?

1 ответ 1

0

Попробуйте у ImageView установить true в adjustViewBounds

В адаптере:

imageView.setAdjustViewBounds(true);

Или в xml:

android:adjustViewBounds="true"

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.