Android:GridView auto_fit如何查找列数?

gal*_*ath 65 android gridview

我想更好地了解Gridview工作方式,特别是auto_fit.这是XML布局:

<?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"
/>
Run Code Online (Sandbox Code Playgroud)

它可以通过一系列六个缩略图(48*48像素)正常工作.在纵向模式下,它显示一行,六列.

同

我不明白为什么这条线android:columnWidth="60dp"是必要的,因为auto_fit应该找到正确数量的列.
如果没有该行android:columnWidth="60dp",它将显示一个3行和2列的网格.

无

这是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
    };
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助.

Vai*_*den 94

查看GridView源代码,很明显在ImageView上设置填充和高度根本无济于事.如果未指定列宽,则只选择预设数量的列(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;
    }

    ...
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

gridView.setColumnWidth( width );
Run Code Online (Sandbox Code Playgroud)

  • 我在哪里需要编写上面的代码? (6认同)
  • measureCellWidth中的视图是什么(上下文上下文,查看单元格)? (2认同)

She*_*tib 7

根据android:numColumns文档

auto_fit 显示尽可能多的列以填充可用空间.

所以,如果你插入ImageViews

padding 调成 zero

margin 调成 zero

layout_width 调成 wrap_content

layout_height 调成 wrap_content

gridView应包含尽可能多的子节点


请记住,您的ImageView可能会缩放(:

  • 是的,我不一定会相信Android文档.有些地方仍然有使用1.0之前版本功能的示例代码! (3认同)