【发布时间】:2009-08-13 15:33:40
【问题描述】:
如何在 android 的 gridview 中指定列跨度?
我有一个网格视图,每行显示 3 个图像。有时,有些图像必须跨越 2 行。
在 Android 中可以做到这一点吗? 还是我应该使用不同的视图?
【问题讨论】:
标签: android gridview html-table
如何在 android 的 gridview 中指定列跨度?
我有一个网格视图,每行显示 3 个图像。有时,有些图像必须跨越 2 行。
在 Android 中可以做到这一点吗? 还是我应该使用不同的视图?
【问题讨论】:
标签: android gridview html-table
TableLayout 和 GridLayout 支持列跨越,但 GridView 不支持。
【讨论】:
GridLayout 之前。
【讨论】:
如果您仍想使用 GridView,您可以隐藏其中一个单元格并扩展其旁边的单元格的宽度,使其跨越整个宽度。这可以在您的 RowAdapter 中完成。
private static LayoutInflater inflater = null;
public class GridRowAdapter extends BaseAdapter
{
private String[] imageURLArray;
public GridRowAdapter(String[] imageURLArray)
{
this.imageURLArray = imageURLArray;
if(inflater == null)
{
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}
public int getCount()
{
int iCount = Math.max(imageURLArray.length, 1);
//Reduce the count of items expected by the GridView because 1 item will take up 2 cells
iCount--;
return iCount;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
try
{
if(parent != null)
{
if(position < 2)
{
v = inflater.inflate(R.layout.grid_item_feature, parent, false);
ViewGroup.LayoutParams params = v.getLayoutParams();
if(position == 0)
{
//Extend height and width of the cell on the left
params.height = (itemsGridView.getWidth());
params.width = (itemsGridView.getWidth());
//Write code to show Image or Text
String strImage = imageURLArray[position];
}
else
{
//You must extend the height of this cell too even though you're going to make it disappear
params.height = (itemsGridView.getWidth());
//Hide cell on the right
v.setVisibility(View.GONE);
}
}
else
{
//Reduce the index of the position because we skipped an item
position--;
v = inflater.inflate(R.layout.grid_item, parent, false);
//Write code to show Image or Text
String strImage = imageURLArray[position];
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return v;
}
}
【讨论】:
这可能会对您有所帮助:
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="4"
android:layout_margin="15dp"
android:background="#DEB887">
<Button android:text="Button 1" />
<Button android:text="Button 2" />
<Button android:text="Button 3" />
<Button android:text="Button 4" />
<Button android:text="Column Span 2"
android:layout_columnSpan="2"
/>
<Button android:text="Button 6" />
<Button android:text="Row Span 2"
android:layout_rowSpan="2"
/>
<Button android:text="Button 8" />
<Button android:text="Button 9" />
<Button android:text="Button 10" />
<Button android:text="Button 11" />
<Button android:text="Button 12" />
<Button android:text="Button 13" />
</GridLayout>
【讨论】: