2012-10-26 42 views
1

我已經將自定義標題添加到Android中的ListView。 Header的佈局xml文件包含一個Image按鈕。我如何添加一個OnClickListener這個ImageButton的如何將OnClickListener添加到Android的ListView標題中的圖像按鈕?

頁眉佈局文件

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

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/add" /> 



</LinearLayout> 

代碼綁定的ListView(我創建了一個自定義適配器)

ListView listView1 = (ListView) findViewById(R.id.listView1); 
    LocationAdapter adapter = new LocationAdapter(this,R.layout.listrow,webresult); 
    View header = (View)getLayoutInflater().inflate(R.layout.listheader, null); 
    listView1.addHeaderView(header); 
    listView1.setAdapter(adapter); 
+0

ImageButton在哪裏? –

+0

對不起,我編輯了這個問題。 –

+1

'header.findViewById(R.id.imageButton1).setOnClickListener(new View.OnClickListener(){@Override public void onClick(View v){/ * ... * /}});' –

回答

1

當你添加一個頭列表視圖它被認爲是您列表的元素0。因此,您不需要添加onClickListener,但可以像列表中的常規行那樣執行此操作:

listview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      if(position==0) //do your stuff 
     } 
    }); 
相關問題