gpt4 book ai didi

java - 自定义 ArrayAdapter 从左下角填充 GridView

转载 作者:行者123 更新时间:2023-12-02 04:40:12 25 4
gpt4 key购买 nike

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

我之前在这里问过一个关于它的问题,并被告知实现这一点的方法是编写一个我已经完成的自定义 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;
}
}

有人可以给我一个简短的介绍,告诉我如何填充网格,以便左下角的单元格是数组的第一个元素吗?

谢谢。

最佳答案

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

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

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

关于java - 自定义 ArrayAdapter 从左下角填充 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30280356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com