繁体   English   中英

如何在Android的ListView标头中将OnClickListener添加到图像按钮?

[英]How to add OnClickListener to image button in the ListView Header in Android?

我已经在Android的ListView中添加了自定义标头。 标头的布局xml文件包含一个“图像”按钮。 如何向此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);

当您将标题添加到listview时,它被视为列表的元素0 因此,您无需添加onClickListener而是像对列表中的常规行那样进行添加:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM