Android:addHeader到ListView

时间:2012-04-11 10:17:51

标签: android listview header

我正在尝试向我的listView添加标题,但在活动启动时我只看到黑屏

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maincoverflow);

        final ListView itemslist = (ListView)findViewById(R.id.itemslist);        
        LayoutInflater inflater = getLayoutInflater();
        ViewGroup mTop = (ViewGroup)inflater.inflate(R.layout.main_header, itemslist, false);
        itemslist.addHeaderView(mTop, null, false);

        //some actions with header and list items
        //example: coverflow_item_title is located in header(R.layout.main_header)
        title = (TextView)findViewById(R.id.coverflow_item_title); //checked - is not null

        CoverFlow coverFlow =  (CoverFlow) findViewById(R.id.coverflow);
        ImageAdapter coverImageAdapter =  new ImageAdapter(this);
        coverFlow.setAdapter(new ImageAdapter(this));
        coverImageAdapter.createImages();

        coverFlow.setAdapter(coverImageAdapter);  
        coverFlow.setOnItemSelectedListener(new CoverAdapterView.OnItemSelectedListener(){           
           ...
           adapter = new LazyAdapter(MainCoverflowActivity.this, tools, tools_images);
           itemslist.setAdapter(adapter); 
        }

        itemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               ...
        }
}

maincoverflow.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/itemslist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

main_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/coverflow_item_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <com.thumbsupstudio.mobitour.CoverFlow
        android:id="@+id/coverflow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/shadowbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/shadow_box" />

</LinearLayout>

你可能会注意到我正在使用Coverflow小部件(从这里http://www.inter-fuser.com/2010/01/android-coverflow-widget.html) - 它运行正常。

通常,当listView项目发生更改时,我正在更改Coverflow内容。它工作正常,但我需要Coverflow滚动listView,所以我决定将它附加到标题。之后,屏幕上没有listViewCoverflow,一切都是黑色的。

2 个答案:

答案 0 :(得分:0)

try this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<RelativeLayout
        android:id="@+id/headerLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>
<TextView
        android:id="@+id/coverflow_item_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <com.thumbsupstudio.mobitour.CoverFlow
        android:id="@+id/coverflow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/shadowbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/shadow_box" />

</RelativeLayout>


    <ListView
        android:id="@+id/itemslist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</RelativeLayout>

答案 1 :(得分:0)

我找到了解决方案。我不明白为什么,但是:

不正确

init listView
inflate header and add it to the list
set list adapter in Coverflow onItemSelected handler (it launch automatically with item position=0)

<强> CORRECT

init listView
inflate header and add it to the list
set any adapter to listView (!!!)
set listView adapter in Coverflow onItemSelected handler (it launch automatically with item position=0)

活动启动后listView具有在Coverflow onItemSelected处理程序中设置的适配器内容。什么是第一个适配器分配 - 我不明白。