[英]How do I create a list of items with equally sized columns in Android
I want to achieve something like this in Android: 我想在Android中实现以下目标:
Note that the content is dynamic, so the width of the columns cannot be fixed at compile time. 请注意,内容是动态的,因此在编译时无法固定列的宽度。 The number of rows is dynamic, however the number of columns is fixed.
行数是动态的,但是列数是固定的。
In WPF I would use an ItemsControl
with a StackPanel
as the ItemsPanel
. 在WPF中,我将使用带有
StackPanel
的ItemsControl
作为ItemsPanel
。 I would then set the ItemTemplate
to a DataTemplate
containing a Grid
with ColumnDefinition
s and SharedSizeGroup
s. 然后,我将
ItemTemplate
设置为一个DataTemplate
其中包含一个带有ColumnDefinition
和SharedSizeGroup
的Grid
。 Something like this: 像这样:
<DataTemplate DataType="MyViewModel" x:Key="MyItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Col0"/>
<ColumnDefinition SharedSizeGroup="Col1"/>
<ColumnDefinition SharedSizeGroup="Col2"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text={Binding Column0} Margin="0 0 5 0"/>
<TextBlock Grid.Column="1" Text={Binding Column1} Margin="0 0 5 0"/>
<TextBlock Grid.Column="2" Text={Binding Column2}/>
</Grid>
</DataTemplate>
<ItemsControl ItemTemplate={StaticResource MyItemTemplate}>
<ItemsControl.ItemsPanel>
<StackPanel Grid.IsSharedSizeScope="True"/>
</ItemsControl>
</ItemsControl>
I cannot figure out how to do it in Android. 我不知道如何在Android中做到这一点。 I know I have to use a
RecyclerView
(the equivalent of an ItemsControl
) and a LinearLayoutManager
(the equivalent of the StackPanel
). 我知道我必须使用
RecyclerView
(相当于ItemsControl
)和LinearLayoutManager
(相当于StackPanel
)。 But how do I match up the column sizes in each item? 但是如何匹配每个项目中的列大小?
You are supposed to use RecyclerView
. 您应该使用
RecyclerView
。 But as a first step, try with GridView
: 但首先,请尝试使用
GridView
:
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:stretchMode="columnWidth"
android:numColumns="3"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:padding="10dp"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.