0

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

ログキャット

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

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 に答える