无法使ListView标头可点击

时间:2013-08-21 13:48:40

标签: android listview android-listview

我正在尝试将标题图片放在listView可点击的顶部。

records.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#efefef"
    android:orientation="vertical" >

    <include layout="@layout/header" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="95dp"
        android:cacheColorHint="#00000000"
        android:divider="@android:color/black"
        android:dividerHeight="1.0sp"
        android:textColor="#000000" />

</RelativeLayout>

header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/headerImage"
    android:layout_width="fill_parent"
    android:layout_height="90dp"
    android:background="@drawable/header"
    android:clickable="true"
    android:scaleType="fitXY" >

</RelativeLayout>

活动内部实施

layoutInflator=(LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        CustomAdapter adapter = new CustomAdapter(this, R.layout.list,R.id.imageStatus, data);
        ListView listView=getListView();


        //LayoutInflater inflater = getLayoutInflater();
        //ViewGroup header = (ViewGroup)layoutInflator.inflate(R.layout.header, listView, false);
        //listView.addHeaderView(header, null, false);
        View itemView = layoutInflator.inflate(R.layout.header,listView);
        ImageView txt = (ImageView)itemView.findViewById(R.id.headerImage);
        txt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(PickupList.this,"header has been pressed", 
                        Toast.LENGTH_LONG).show();

            }
        });

        setListAdapter(adapter);

logcat的

08-21 19:11:51.919: E/AndroidRuntime(22191): FATAL EXCEPTION: main
08-21 19:11:51.919: E/AndroidRuntime(22191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.myapp/com.android.myapp.PickupList}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.os.Looper.loop(Looper.java:130)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.app.ActivityThread.main(ActivityThread.java:3687)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at java.lang.reflect.Method.invokeNative(Native Method)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at java.lang.reflect.Method.invoke(Method.java:507)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at dalvik.system.NativeStart.main(Native Method)
08-21 19:11:51.919: E/AndroidRuntime(22191): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.widget.AdapterView.addView(AdapterView.java:461)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:416)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at com.android.myapp.PickupList.ShowListOnUI(PickupList.java:103)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at com.android.myapp.PickupList.onCreate(PickupList.java:71)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-21 19:11:51.919: E/AndroidRuntime(22191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-21 19:11:51.919: E/AndroidRuntime(22191):    ... 11 more

2 个答案:

答案 0 :(得分:2)

请,请不要尝试自己将View放入ListView。在发布之前,使用已注释掉的ListView的内置addHeaderView方法和 PLEASE read the documentation

在文档中,它明确指出最后一个布尔参数确定标题视图是否可点击。在上面的代码中,您将其设置为false

答案 1 :(得分:0)

headerImage是一个相对布局,你可以只使用标准视图类(每个视图都继承自)并仍然使用setOnClickListener方法

    View txt = itemView.findViewById(R.id.headerImage);
    txt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // use the getContext() method from the v param
            Toast.makeText(v.getContext(),"header has been pressed", 
                    Toast.LENGTH_LONG).show();

        }
    });