2012-04-11 11 views
0

Ich versuche, einen Header zu meinem listView hinzufügen, aber ich sehe nur schwarzer Bildschirm, wenn die Aktivität startetAndroid: addHeader zu 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> 

Sie werden feststellen, dass ich Coverflow Widget bin mit (von hier http://www.inter-fuser.com/2010/01/android-coverflow-widget.html) - es funktioniert gut.

Im Allgemeinen ändere ich listView Inhalt, wenn Coverflow Element geändert wird. Es funktioniert gut, aber ich brauche , um mit listView zu scrollen, also entschied ich mich, es an den Header zu hängen. Danach gibt es keine listView und Coverflow auf dem Bildschirm, alles ist schwarz.

+0

Haben Sie Ihren Adapter nach dem Hinzufügen der Kopfzeile überprüft? Wenn Sie eine Kopfzeile hinzufügen, werden die Positionen der Elemente aufgrund der Kopfzeile um 1 erhöht. Ich hoffe, dass das deine Liste nicht durcheinander bringt. – Shubhayu

+0

Haben Sie versucht, hierarchyviewer zu sehen, wo Ihre Artikel positioniert sind? – njzk2

+0

@Shubhayu Ich setze Listenadapter nach dem Hinzufügen der Kopfzeile. Ich denke, wenn eine Positionsnummernverschiebung stattfindet und es wichtig ist, dann sollte ein Fehler auftreten, also habe ich nur schwarzen Bildschirm –

Antwort

0

Ich fand eine Lösung. Ich verstehe nicht ganz, warum, aber:

nicht korrekt

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

RICHTIG

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) 

nach Aktivität startet listView Adapter Inhalt hat, die in Coverflow onItemSelected Handler gesetzt wurde. Was für ist die erste Adapterzuweisung - ich verstehe nicht.

+0

Ich hatte dies gestern völlig übersehen! Solange und bis Sie den Adapter festgelegt haben, wird die Liste nicht generiert. Sie erstellen den Coverflow und legen ihn als Kopfzeile fest, aber das Festlegen des Adapters der Liste, die den Coverflow-Header enthält, geschieht nur in onItemSelected(). Was im Grunde bedeutet, dass die Liste nie an erster Stelle erstellt wird! – Shubhayu

+0

Ja, jetzt ist es klar –

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

ich denke, das Problem hier ist, die Kopfzeile mit der Listenansicht zu scrollen. – njzk2

+0

ja, ich brauche auch Header scrollen. Ich denke, dass Code es nicht zulassen wird –