建立和处理具有不同行列表视图的最佳方法 [英] Best way to build and handle List view with differing rows
问题描述
在我的应用程序,我查看列表中查看数据。它看起来如下。
In my app, I show data in a list view. It looks as below.
在列表视图中的每一行是不同的(即,它们有不同的看法结构)。但是,每一行是由1对多子视图(所有子视图都是一样的)。
Each row in list view is different (ie., they have a different view structure). But every row is made up of 1 to many subviews (all subview are same ).
问题:如果试图从充气然后XML我需要有XML与行
1. 2子视图
2. 3子视图
3. 5子视图等等
Problem: If try to inflate the rows from xml then I need to have xml with 1. 2 subviews 2. 3 subviews 3. 5 subviews and so on
如果我尝试建立的行动态那么我不会能够采取持有者图案或意见的ListView重用性的优势。我将最终构建子视图如果子视图的数量,在返回的视图小于要求或情况下,删除子视图比要求的多。
If I try to build the rows dynamically then I wont be able to take advantage of the holder pattern or the views re-usability of Listview. And I will end up building the subviews if the number of subviews, in the returned view is less than required or deleting in case the subviews are more than required.
在简单地说,我怎么能在列表视图不同的看法这并不妨碍看法:复用性或支架模式?
In a nutshell, how can I have different views in listview which does not hamper view re-usability or holder pattern ?
编辑:
我已经扩展ArrayAdapter和重写getView,getViewTypeCount,getItemViewType
I have extended ArrayAdapter and overriden getView, getViewTypeCount, getItemViewType
@Override
public int getItemViewType(int position) {
return mListItems.get(position).getViewToBeRenderened().getViewId();
}
@Override
public int getViewTypeCount() {
return AppConstants.VwViewTypes.values().length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final VWListItem listItem = mListItems.get(position);
if(listItem == null || listItem.getViewToBeRenderened() == null){
return convertView;
}
VWView renderingView = listItem.getViewToBeRenderened();
if(convertView == null ){
convertView = renderingView.buildView(mContext.appContext);
}
...
}
本办法制定最初的时候,只有2-3排不同的看法。对于这些意见行我有一个不同的XML布局,这将建立和膨胀相应的类。
This approach worked initially when there were only 2-3 differing row views. For each of these row views i had a different xml layout and corresponding class which would build and inflate.
但现在这种观点的人数越来越多我和不想有每增加一个新行图的布局和类。相反,我想重用现有的布局和类,并建立他们。如果有一个更好的解决方案,然后我也开来了。
But now the number of such views is growing and I dont want to have a layout and class for each additional new row view. Instead I would like to reuse existing layouts and class and build on them. If there is a better solution then am open to it as well.
推荐答案
当你必须管理各种不同的意见,你应该重写 getViewTypeCount()
和 getItemViewType()
,您将收到一些convertViews的等于通过的getViewTypeCount()
。例如,如果返回2,你会得到2型动物 convertView
。
when you have to manage different kinds of views you should override getViewTypeCount()
and getItemViewType()
and you will receive a number of convertViews equals to the int returned by getViewTypeCount()
. For instance, If it returns 2 you will get 2 differents convertView
.
这篇关于建立和处理具有不同行列表视图的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!