0

布局

我想使用 listvview 完成上述布局。我有几个部分(动态生成)和几个项目。我需要将每个部分封装在自己的背景上,但我需要用户能够像普通列表视图一样滚动所有内容,而不仅仅是较小的部分。(因此排除了为每个部分实施 N-listviews)

所以我的问题是你将如何构建这样的布局?

4

3 回答 3

1

这并不难。我用 ListView 完成了这个,它正在处理回收。在这段代码中,它将为每个列表项在绿色和红色之间切换颜色。

@Overrid
public int getItemViewType(int position) {
        if((position % 2) == 0) {
            return TYPE_GREEN;
        } else {
            return TYPE_RED;;
}

@Override
public int getViewTypeCount() {
    return numberOfColors;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int type = getItemViewType(position);

    if (type == TYPE_RED) {
        convertView = mInflater.inflate(R.layout.list_item_red,
                null);
    } else if (type == TYPE_GREEN) {
        convertView = mInflater.inflate(R.layout.list_item_green,
                null);
    }

}
于 2013-06-07T22:40:02.117 回答
0

最简单的解决方案是根本不使用 ListView。使用一系列 LinearLayout 容器,手动添加项目。请记住,此选项不会使用视图回收,并且对于长列表将是性能灾难。

如果性能问题阻止了 LinearLayouts,您将不得不对列表项进行一些巧妙的操作以实现您的目标。您可以通过将列表中所有项目的背景设置为使用相似的背景,然后在内部视图中偏移项目的内容,使这些部分看起来具有连续的背景。您将填充一个部分的第一个项目的顶部、一个部分中最后一个项目的底部以及所有项目的侧面。为了保持这种错觉,请移除 ListView 分隔符并在列表项内的视图中设置一个。一旦您开始与列表交互,选择器会看起来很有趣,因此您可能想要自定义它。

                                  ---
|-------------------------|        |
|#########################|        |
|##|-------------------|##|        |
|##|                   |##|  Actual list item
|##|                   |##|        |
|##|                   |##|        |
|##|-------------------|##|       ---
|##|                   |##|        |
|##|                   |##|  Actual list item
|##|                   |##|        |
|##|-------------------|##|       ---
|##|                   |##|        |
|##|                   |##|        |
|##|                   |##|  Actual list item
|##|-------------------|##|        |
|#########################|        |
|-------------------------|        |
                                  ---  
|-------------------------|        |
|@@@@@@@@@@@@@@@@@@@@@@@@@|        |
|@@|-------------------|@@|        |
|@@|                   |@@|  Actual list item
|@@|                   |@@|        |
|@@|                   |@@|        |
|@@|-------------------|@@|       ---
|@@|                   |@@|        |
|@@|                   |@@|        |
|@@|                   |@@|  Actual list item
|@@|-------------------|@@|        |
|@@@@@@@@@@@@@@@@@@@@@@@@@|        |
|-------------------------|        |
                                  ---  
于 2013-06-07T21:31:02.007 回答
0

对于你的 ref ,我在下面的应用程序中实现了同样的事情

这就是我的实现方式,1)将所有数据保存在一个 Arraylist 中。例如:

private String HEADING_TAG="heading";
private String ITEM_TAG="item" ;
ArrayList<String> data=
new ArrayList<String>();
data.add( HEADING_TAG+"@selector");
data.add( ITEM_TAG+ "@item1")
/* in the above line "heading" is a tag, just to know that the 
following listitem is a heading.
"@" is a separator using which we wl split the string later in 
the adapter*/

2) 现在实现一个自定义适配器并覆盖 getView() 方法。注意:将列表的长度传递给适配器。3) 在getView() 方法中访问数据列表,getView 方法中会出现一个叫做位置的参数。使用该变量来迭代列表。例如:

getView(....,....,int pos,....)
{
String temp[]= data.get(position).split("@");
if(temp[0].equals( HEADING_TAG ))
{
// change the background to the layout which u r inflating.
// Set the heading to the textview or what ever view u defined
//the layout
}
else if (temp[1].equals( ITEM_TAG ) )
{
// set the item to the view
}
}

希望这可以帮助...

于 2013-06-07T21:35:05.473 回答