自定义ArrayAdapter以从左下角填充GridView

时间:2015-05-16 20:21:08

标签: java android gridview android-arrayadapter

我有一个GridView,我正在使用wordsearch应用程序,我正在填充哪个字符串数组。但是我想从左下角开始填充它,然后向上填充而不是默认的左上角。

我之前在这里问了一个关于它的问题并被告知实现这个目的的方法是编写一个我已经完成的自定义ArrayAdapter,但是我对Android很新,并且不知道如何让网格填充从适配器的左下角开始。

我还需要能够获取点击的网格中项目的元素编号,类似于我现在正在做的事情:

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;
    }
}

有没有人能给我一个简短的介绍,我将如何让网格填充,以便左下角的单元格是数组的第一个元素?

感谢。

2 个答案:

答案 0 :(得分:2)

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

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

通过在GridView布局XML中使用android:stackFromBottom =“false”元素,我解决了这个问题。 FYI。

答案 1 :(得分:0)

在大部分时间都花了这么多的时间后,我设法找到了解决问题的方法,不确定这是否是最佳方式,但它的工作方式与我需要的完全一样。

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;
    }
}