我如何选择convertview重用? [英] how do i choose convertview to reuse?
问题描述
上下文
我想要一个列表项具有3种明显不同的布局的列表项,因此我根据要显示的项的类型来创建适配器,以创建适当的视图.
例如我想列出一些图像,文本和数字,每个都有一些标题.
我知道在
public View getView(int position, View convertView, ViewGroup parent)
convertView
表示重复使用不再可见的listItems视图.
Context
I want to have a list with 3 significantly different layouts for list items so I make my adapter that based on type of item to display creates appropriate view.
e.g. i want to list some images, texts and numbers, each with some title.
I know that in
public View getView(int position, View convertView, ViewGroup parent)
the convertView
stands for reusing no longer visible listItems views.
问题
如何选择convertView
或如何控制进入那里的东西?
Question
How can I choose the convertView
or how can i control what i get in there?
问题来自于不同的listItems视图,假设我的列表以图像listItem开头,然后出现很多文本listItems和数字listItems,而后面有100个listItems是第二个图像.
我假设向下滚动列表(在getView(...)
调用中)不为null的第一个convertView
是带有图像的图像,并且由于我需要显示文本listItem或数字listItem的视图,因此无法使用它.然后我想在每个下一个getView(...)
调用中,convertView
都是与先前调用中相同的图像listItem,因为我以前没有使用过它.
The problem comes with different listItems views, assume I have my list starting with an image listItem, then comes a lot of text listItems and number listItems and 100 listItems later comes second image.
I assume that while scrolling down the list, (in getView(...)
calls) the first convertView
that is not null is the one with image, and since i need a view to display text listItem or number listItem I cannot use it. Then i guess that in every next getView(...)
call the convertView
is that same image listItem as in previous calls because i didn't use it before.
未使用的文本listItems和数字listItems卡住了,滚动列表时,我需要继续创建新视图,这是我要防止的事情.
The unused text listItems and number listItems stuck up and when scrolling the list I need to keep creating new views, this is what i want to prevent.
推荐答案
您需要让适配器的视图回收器知道不止一种布局,以及如何区分每一行的两种布局.只需覆盖以下方法:
You need to let the adapter's view recycler know that there is more than one layout and how to distinguish between the two for each row. Simply override these methods:
我在这里说了2种不同的布局.如果您还有更多,请使用枚举来区分它们.
Here i have said for 2 different layouts. If you have more use an enum to distinguish them.
@Override
public int getItemViewType(int position) {
// Define a way to determine which layout to use, here it's just evens and odds.
return position % 2;
}
@Override
public int getViewTypeCount() {
return 2; // Count of different layouts (Change according to your requirment)
}
将getItemViewType()合并到getView()中,如下所示:
Incorporate getItemViewType() inside getView(), like this:
if (convertView == null) {
// You can move this line into your constructor, the inflater service won't change.
mInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
if(getItemViewType(position) == 0)
convertView = mInflater.inflate(R.layout.listview_item_product_1, parent,false);
else
convertView = mInflater.inflate(R.layout.listview_item_product_2,parent,false);
// etc, etc...
在Google Talks上观看Android的Romain Guy讨论查看回收站.
Watch Android's Romain Guy discuss the view recycler at Google Talks.
这篇关于我如何选择convertview重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!