1

我在 android 的 ListView 中添加了一个自定义标题。Header 的布局 xml 文件包含一个 Image 按钮。如何向此 ImageButton 添加 OnClickListener

标题布局文件

<?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);
4

1 回答 1

1

当您向列表视图添加标题时,它被视为0列表的元素。因此,您无需添加 an,onClickListener而是像使用列表中的常规行一样执行此操作:

    listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          if(position==0) //do your stuff
        }
    });
于 2012-10-26T10:15:53.530 回答