【问题标题】:Customise ArrayAdapter to fill GridView from bottom-left自定义 ArrayAdapter 从左下角填充 GridView
【发布时间】:2015-05-16 20:21:08
【问题描述】:

我有一个 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;
    }
}

谁能简要介绍一下我将如何填充网格以使左下角的单元格成为数组的第一个元素?

谢谢。

【问题讨论】:

    标签: java android gridview android-arrayadapter


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,不幸的是我在交手后发现了这个哈哈,最后写了一个非常烦人的算法来切换每个条目的位置。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      相关资源
      最近更新 更多