I have a list activity that creates a header row above the data rows from the adapter. I want to receive click events when the user selects the header or a data row (but my data rows have check boxes in them, so this part is tricky). When I use the touch screen, I get this expected behavior:
1. Tapping on any row causes a click event and a dialog appears. 2. Long-pressing on a data row causes a context menu to appear. 3. Long-pressing on the header row does *not* show the context menu. However, when I use the arrow keys (emulator) or trackball (G1), I get this unexpected behavior: 1. Selecting any row fails to cause any click events, even though the row's appearance changes like it's being clicked. 2. Long-pressing on the header row *does* show the context menu, which I don't want. 3. Occasionally, it doesn't focus the correct row when I move up or down (e.g. it skips from the header to the last data row). What am I doing wrong? Jesse (Complete project: http://hansprestige.com/android/DpadBug.zip) ////////// DpadActivity.java ////////// package com.hansprestige.DpadBug; import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class DpadBugActivity extends ListActivity { private static final int DIALOG_TEST = 1; private OnClickListener testListener = new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_TEST); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lv = getListView(); registerForContextMenu(lv); findViewById(android.R.id.empty).setOnClickListener (testListener); View addItem = getLayoutInflater().inflate (R.layout.list_header, null, false); addItem.setOnClickListener(testListener); lv.addHeaderView(addItem); fillData(); } private void fillData() { String[] items = new String[] { "Item A", "Item B", "Item C" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_row, R.id.TextView01, items) { @Override public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); v.setOnClickListener(testListener); v.setLongClickable(true); return v; } }; setListAdapter(adapter); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_TEST: return new AlertDialog.Builder(this) .setTitle("Hello!") .setPositiveButton("Close", null) .create(); } return super.onCreateDialog(id); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add("Context item"); } } ////////// main.xml ////////// <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:focusable="false" /> <include android:id="@+id/android:empty" layout="@layout/ list_header" /> </LinearLayout> ////////// list_header.xml ////////// <?xml version="1.0" encoding="UTF-8"?> <!-- This seems way too complicated, but the outer view's layout params are apparently ignored, so I don't see a simpler way to (1) center the text vertically in a large row and (2) make the background highlight fill the entire row. --> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/ android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:drawable/menuitem_background" android:focusable="true" android:descendantFocusability="blocksDescendants"> <FrameLayout android:layout_width="99999dip" android:layout_height="50dip"> <TextView android:text="List header view" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dip"></TextView> </FrameLayout> </FrameLayout> ////////// list_row.xml ////////// <?xml version="1.0" encoding="UTF-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" xmlns:android="http://schemas.android.com/apk/res/android" android:clickable="true" android:focusable="true" android:background="@drawable/ android:menuitem_background" android:layout_width="fill_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants"> <CheckBox android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/CheckBox01" android:layout_marginTop="1dip" android:layout_marginRight="2.5dip" android:layout_marginLeft="1dip"></CheckBox> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:id="@+id/TextView01" android:text="Top left" android:textStyle="bold" android:layout_toRightOf="@+id/CheckBox01" android:layout_marginTop="5dip" android:focusable="false"></TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom left" android:layout_alignLeft="@+id/ TextView01" android:id="@+id/TextView02" android:focusable="false" android:layout_below="@+id/TextView01"></TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top right" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:id="@+id/TextView03" android:textStyle="bold" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:focusable="false"></ TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom right" android:id="@+id/TextView04" android:layout_below="@+id/TextView03" android:layout_alignRight="@ +id/TextView03" android:focusable="false"></TextView> </RelativeLayout> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---