[英]Android GridView with different row number
I need to use a gridview and I want to change row numbers dynamically. 我需要使用gridview,我想动态更改行号。
Example: 例:
I need something like this: 我需要这样的东西:
I receive an ArrayList with the result, I need to show first ten results on single row and results from eleven to end on two rows layout. 我收到一个带有结果的ArrayList,我需要在单行上显示前10个结果,从11个结果在两行布局上结束。
is it possible to do? 有可能吗?
To accomplish this , you can use listview with custom adapter, you need to create 2 custom listview item with one column, and second one with 2 column. 要实现此目的,您可以将listview与自定义适配器一起使用,您需要使用一列创建2个自定义列表视图项,并使用2列创建第二个自定义列表视图项。
then use 然后用
getViewTypeCount()
which will return 2 in your case. 在你的情况下将返回2。
and in getView()
check the type with 并在
getView()
检查类型
getItemViewType(int position)
and create the appropriate view and return it. 并创建适当的视图并返回它。
对于前10个项目,您可以使用两个不同的网格视图(单行),对于11个结束项目,可以使用第二个(有两行)。
I would create two layouts, both being GridView
, one with one column and the other with two columns. 我会创建两个布局,都是
GridView
,一个是一列,另一个是两列。 I would suggest using a ListView
for the first as it is only one column but you cannot put an unscrollable ListView
inside a ScrollView
. 我建议首先使用
ListView
,因为它只有一列,但你不能在ScrollView
放置一个不可ScrollView
ListView
。 I see you said you've tried this but it doesn't look very nice as they scroll separately. 我看到你说你已经尝试过了,但它们看起来并不是很好,因为它们分别滚动。
Try adding a ScrollView
around both GridView
s and then disable the scrollability of the items inside and then it should scroll as one item. 尝试在
GridView
周围添加ScrollView
,然后禁用内部项目的可滚动性,然后它应滚动为一个项目。
Here is the code for the ScrollView: 以下是ScrollView的代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
and then obviously end it at the end of your XML with this: 然后显然在XML的末尾结束它:
</ScrollView>
After doing that, you should then disable the scrollability of both the GridView
s using this line: 执行此操作后,您应该使用以下行禁用
GridView
的可滚动性:
gridView.setScrollContainer(false);
I hope this helps! 我希望这有帮助! Ask if you need anything.
询问您是否需要任何东西。
This is my layout code. 这是我的布局代码。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Listado" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lvDestacadas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
tools:listitem="@layout/celda" >
</ListView>
<GridView
android:id="@+id/gvGeneral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="2dip"
android:numColumns="2"
android:verticalSpacing="2dip"
tools:listitem="@layout/celda_grid" >
</GridView>
</LinearLayout>
If it always needs to be done this way ie always first 10 will be in a single row then you can do it with custom adapter. 如果它总是需要以这种方式完成,即总是前10个将在一行中,那么你可以使用自定义适配器。 Override getView, you get position as arguement return the view as required.
覆盖getView,您可以根据需要返回视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.