自定义 ArrayAdapter 从左下角填充 GridView

Customise ArrayAdapter to fill GridView from bottom-left

我有一个用于单词搜索应用程序的 GridView,我正在填充一个字符串数组。但是我想从左下角穿过然后向上填充它,而不是默认的左上角向下填充它。

我早些时候在这里问了一个问题,有人告诉我实现这个的方法是编写一个我已经完成的自定义 ArrayAdapter,但是我是 Android 的新手,不知道如何实现让网格从适配器的左下角填充。

我还需要能够获取被点击的网格中item的元素编号,类似于我现在做的:

position = wordsearchGrid.pointToPosition((int)X, (int)Y);

这是我目前为自定义适配器编写的代码,它可以工作,但显然仍然从左上角填充:

public class GridAdapter extends ArrayAdapter
{
    Context context;
    Object[] items;
    int resource;

    LayoutInflater inflater;

    public GridAdapter(Context context, int resource, Object[] items) 
    {
        super(context, resource, items);
        this.context = context;
        this.resource = resource;
        this.items = items;

        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() 
    {
        return items.length;
    }

    @Override
    public Object getItem(int position) 
    {
        return items[position];
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
            TextView cell;

            int cellWidth = 40;
            int cellHeight = 50;


            if (convertView == null) 
            {
                // If convertView is null then inflate the appropriate layout file
                convertView = inflater.inflate(resource, null);
            }

            cell = (TextView) convertView.findViewById(R.id.gridCell);

            cell.setText((CharSequence) items[position]);

            // Set height and width constraints for the image view
            cell.setLayoutParams(new LinearLayout.LayoutParams(cellWidth, cellHeight));

            // Set Padding for images
            cell.setPadding(1, 1, 1, 1);

            return convertView;
    }
}

谁能给我一个简短的 运行-through 我将如何填充网格以便左下角的单元格是数组的第一个元素?

谢谢。

在这上面花了大半夜之后,我设法找到了解决方法,不确定这是否是最好的方法,但它完全符合我的需要。

public class GridAdapter extends ArrayAdapter<Object>
{
    Context context;
    Object[] items;
    int resource;
    int puzzleWidth;
    int newPosition;
    LayoutInflater inflater;
    SparseIntArray positionMap = new SparseIntArray();

    public GridAdapter(Context context, int resource, Object[] items, int puzzleWidth) 
    {
        super(context, resource, items);
        this.context = context;
        this.resource = resource;
        this.items = items;
        this.puzzleWidth = puzzleWidth;

        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() 
    {
        return items.length;
    }

    @Override
    public Object getItem(int position) 
    {
        return items[position];
    }

    @Override
    public long getItemId(int position) 
    {
        //returns the new position
        return positionMap.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
            TextView cell;

            int cellWidth = 40;
            int cellHeight = 40;

            //works out the row number by rounding down position/puzzleWidth
            long rowNumber = (long)position / puzzleWidth;

            //changes position from top down to bottom up
            switch ((int)rowNumber)
            {
                case 0:
                    newPosition = position + ((puzzleWidth - 1) * puzzleWidth);
                    break;
                case 1:
                    newPosition = position + ((puzzleWidth - 3) * puzzleWidth);
                    break;
                case 2:
                    newPosition = position + ((puzzleWidth - 5) * puzzleWidth);
                    break;
                case 3:
                    newPosition = position + ((puzzleWidth - 7) * puzzleWidth);
                    break;
                case 4:
                    newPosition = position + ((puzzleWidth - 9) * puzzleWidth);
                    break;
                case 5:
                    newPosition = position + ((puzzleWidth - 11) * puzzleWidth);
                    break;
                case 6:
                    newPosition = position + ((puzzleWidth - 13) * puzzleWidth);
                    break;
                case 7:
                    newPosition = position + ((puzzleWidth - 15) * puzzleWidth);
                    break;
                case 8:
                    newPosition = position + ((puzzleWidth - 17) * puzzleWidth);
                    break;
                case 9:
                    newPosition = position + ((puzzleWidth - 19) * puzzleWidth);
                    break;
                case 10:
                    newPosition = position + ((puzzleWidth - 21) * puzzleWidth);
                    break;
                case 11:
                    newPosition = position + ((puzzleWidth - 23) * puzzleWidth);
                    break;
                case 12:
                    newPosition = position + ((puzzleWidth - 25) * puzzleWidth);
                    break;
                case 13:
                    newPosition = position + ((puzzleWidth - 27) * puzzleWidth);
                    break;
                case 14:
                    newPosition = position + ((puzzleWidth - 29) * puzzleWidth);
                    break;
            }

            //store the old and new positions
            positionMap.put(position, newPosition);

            if (convertView == null) 
            {
                // If convertView is null then inflate the appropriate layout file
                convertView = inflater.inflate(resource, null);
            }

            cell = (TextView) convertView.findViewById(R.id.gridCell);

            cell.setText((CharSequence) items[newPosition]);

            // Set height and width constraints for the textview
            cell.setLayoutParams(new LinearLayout.LayoutParams(cellWidth, cellHeight));

            // Set Padding for images
            cell.setPadding(1, 1, 1, 1);

            return convertView;
    }
}

我在自定义 ArrayAdapter 时遇到了同样的问题。查看GridView后发现它是从AbsListView派生出来的,并且有android:stackFromBottom属性指定是否从底部堆叠内容。

<GridView ...
    android:stackFromBottom="false"
/>

通过在 GridView 布局 XML 中使用 android:stackFromBottom="false" 元素,我解决了问题。仅供参考。