Android:GridView auto_fit 如何求列数? [英] Android: How does GridView auto_fit find the number of columns?

查看:12
本文介绍了Android:GridView auto_fit 如何求列数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地了解 Gridview 的工作原理,尤其是 auto_fit.这是 XML 布局:

I would like to understand better how Gridview works, in particular auto_fit. Here is the XML layout:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:columnWidth="60dp"
    android:numColumns="auto_fit"
/>

它适用于一系列六个缩略图(48*48 像素).在纵向模式下,它显示一行六列.

And it works fine with a series of six thumbnails (48*48 pixels). In portrait mode, it displays one row, six columns.

我不明白为什么行 android:columnWidth="60dp" 是必要的,因为 auto_fit 预计会找到正确的列数.
如果没有 android:columnWidth="60dp" 行,它会显示一个 3 行 2 列的网格.

What I don't understand is why the line android:columnWidth="60dp" is necessary, because auto_fit is expected to find the right number of columns.
Without the line android:columnWidth="60dp", it displays a grid 3 rows and 2 columns.

这是 ImageAdapter 类:

package com.examples.HelloGridView;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.ic_1, R.drawable.ic_2,
            R.drawable.ic_3, R.drawable.ic_4,
            R.drawable.ic_5, R.drawable.ic_6
    };
}

感谢您的帮助.

推荐答案

查看 GridView 源码,很明显在你的 ImageView 上设置 padding 和 height 对你一点帮助都没有.当没有指定列宽时,它只是选择预设的列数(2):

Looking at the GridView source, it is clear that setting the padding and the height on your ImageView will not help you at all. When a column width is not specified, it just chooses a preset number of columns (2):

    private void determineColumns(int availableSpace) {

    ...

    if (mRequestedNumColumns == AUTO_FIT) {
        if (requestedColumnWidth > 0) {
            // Client told us to pick the number of columns
            mNumColumns = (availableSpace + requestedHorizontalSpacing) /
                    (requestedColumnWidth + requestedHorizontalSpacing);
        } else {
            // Just make up a number if we don't have enough info
            mNumColumns = 2;
        }
    } else {
        // We picked the columns
        mNumColumns = mRequestedNumColumns;
    }

    if (mNumColumns <= 0) {
        mNumColumns = 1;
    }

    ...

解决方案是在设置 GridView 的列宽之前测量您的列大小.这是一种在屏幕外测量视图的快速方法:

The solution is to measure your column size before setting the GridView's column width. Here is a quick way to measure Views offscreen:

public int measureCellWidth( Context context, View cell )
{

    // We need a fake parent
    FrameLayout buffer = new FrameLayout( context );
    android.widget.AbsListView.LayoutParams layoutParams = new  android.widget.AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    buffer.addView( cell, layoutParams);

    cell.forceLayout();
    cell.measure(1000, 1000);

    int width = cell.getMeasuredWidth();

    buffer.removeAllViews();

    return width;
}

然后你只需设置 GridView 的列宽:

And then you just set the GridView's column width:

gridView.setColumnWidth( width );

这篇关于Android:GridView auto_fit 如何求列数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆