0

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

记录.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);

日志猫

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
4

2 回答 2

2

请,请不要尝试自己将 View 膨胀到 ListView 中。addHeaderView使用您已注释掉的 ListView的内置方法,并在发布之前 阅读文档...

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

于 2013-08-21T14:06:28.190 回答
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();

        }
    });
于 2013-08-21T13:52:20.100 回答