ListView控件的混合simple_list_item_1管理和simple_list_item_2 [英] ListView that mixes simple_list_item_1 and simple_list_item_2
问题描述
我想用 simple_list_item_2
它显示的姓名和信息有关每个项目一个ListView,所以它看起来是这样的:
I want a ListView which displays name and info about each item using simple_list_item_2
, so it looks something like this:
SWITZERLAND
a country with many delicious cheeses
BRITAIN
famous for its clocktower
AUSTRALIA
in Australia, everything is upside down!
在盖文的目的是重新present中的每个元素的ListView大的黑色文本和下盖文重presents灰色,小类型的元素。
The text in caps is meant to represent the large black text in each ListView element and the lower caps text represents the grey, small type of that element.
不过,我希望用户可以添加自己的国家,因此第一个元素会显示为
However, I want the user to be able to add his own countries, so the first element would show up as
ADD NEW COUNTRY...
在没有任何小类型的文本。我可以使用 simple_list_item_2
键,只需将小文,
,但这种笨拙对齐大文本。我想这种特殊的添加新的国家元素,看起来就像 simple_list_item_1管理
。
Without any small-type text. I can use simple_list_item_2
and simply set the small text to ""
, but this aligns the large text awkwardly. I want this special "add new country" element to look exactly like simple_list_item_1
.
然而,决定使用 simple_list_item_1管理
或 simple_list_item_2
在我的适配器的初始化是由:
However, the decision to use simple_list_item_1
or simple_list_item_2
is made in the initialization of my adapter:
MyAdapter adapter = new MyAdapter(
this,
android.R.layout.simple_list_item_2,
myListOfListViewElements);
,然后当我填充的ListView这个决定实现:
And then this decision is realized when I populate the ListView:
myListView.setAdapter(adapter);
因此,如何我可以用这两种simple_list_item于一体的ListView?
So how can I use both kinds of simple_list_item in one ListView?
推荐答案
对于那些你正在处理只是一个单一的特别项目的具体情况,@ dymmeh的答案肯定是一个很好的,易于实现的方法。对于更一般的情况下,如果你需要处理的不一定坐在的 ListView的,你可以参考在以下两种方法顶部的多个不同项目适配器:
For the specific case where you're dealing with just a single 'special' item, @dymmeh's answer is definitely a good and easy to implement approach. For the more generic case, where you need to deal with multiple different items that need not necessarily sit at the top of the ListView
, you can refer to the following two methods in an adapter:
-
getItemViewType(INT位置)
-
getViewTypeCount()
getItemViewType(int position)
getViewTypeCount()
根据第一种方法的返回值,Android将养活你所期望的 convertView
在适配器的 getView(...)
方法。一种常见的方法是简单地返回布局ID类型(如 R.layout.row_layout
),但你真的可以用任何你喜欢的,所提供的数值唯一标识类型
Based on the return value of the first method, Android will feed you the desired convertView
in the adapter's getView(...)
method. A common approach is to simply return the layout ids as type (e.g. R.layout.row_layout
) , but you can really use anything you like, provided the values uniquely identify a type.
在这之后,你需要做额外的工作一点点构建您的ViewHolder / RowWrapper时膨胀正确的布局。对于每个视图类型,你需要选择合适的布局,如果 convertView == NULL
。将有至少一个视图类型(这是默认的情况),最多 getViewTypeCount()
。 说明想法的例子可以在这里找到
After that, you'll need to do a little bit of extra work to inflate the correct layouts when constructing your ViewHolder/RowWrapper. For each view type, you'll need to pick the right layout if convertView == null
. There will be at least one view type (which is the default case) and at most getViewTypeCount()
. An example illustrating the idea can be found here.
视图类型方法的详细信息:
More details on the view type methods:
- getViewTypeCount和ArrayAdapter 的getItemViewType方法
- getViewTypeCount and getItemViewType methods of ArrayAdapter
在一个侧面说明:其实我考虑不使的ListView
的添加...选项的一部分,而是在顶部或上述立场这使得它的方便访问;即无需回滚动到顶部第一。
On a side note: I'd actually consider not making the 'add...' option part of the ListView
, but rather position it on top of or above it so that it's conveniently accessible; i.e. no need to scroll back to the top first.
这篇关于ListView控件的混合simple_list_item_1管理和simple_list_item_2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!