My list rows are clickable with touch but not with trackball

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

My list rows are clickable with touch but not with trackball

Postby Mr2001 » Sun Mar 08, 2009 11:57 am

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?

DpadBugActivity.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.hansprestige.DpadBug;
  3.  
  4.  
  5.  
  6. import android.app.AlertDialog;
  7.  
  8. import android.app.Dialog;
  9.  
  10. import android.app.ListActivity;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.view.ContextMenu;
  15.  
  16. import android.view.View;
  17.  
  18. import android.view.ViewGroup;
  19.  
  20. import android.view.ContextMenu.ContextMenuInfo;
  21.  
  22. import android.view.View.OnClickListener;
  23.  
  24. import android.widget.ArrayAdapter;
  25.  
  26. import android.widget.ListView;
  27.  
  28.  
  29.  
  30. public class DpadBugActivity extends ListActivity {
  31.  
  32.         private static final int DIALOG_TEST = 1;
  33.  
  34.  
  35.  
  36.     private OnClickListener testListener = new OnClickListener() {
  37.  
  38.         public void onClick(View v) {
  39.  
  40.                 showDialog(DIALOG_TEST);
  41.  
  42.         }
  43.  
  44.     };
  45.  
  46.  
  47.  
  48.     @Override
  49.  
  50.     public void onCreate(Bundle savedInstanceState) {
  51.  
  52.         super.onCreate(savedInstanceState);
  53.  
  54.         setContentView(R.layout.main);
  55.  
  56.  
  57.  
  58.         ListView lv = getListView();
  59.  
  60.         registerForContextMenu(lv);
  61.  
  62.  
  63.  
  64.         findViewById(android.R.id.empty).setOnClickListener(testListener);
  65.  
  66.  
  67.  
  68.         View addItem = getLayoutInflater().inflate(R.layout.list_header,
  69.  
  70.                         null, false);
  71.  
  72.         addItem.setOnClickListener(testListener);
  73.  
  74.         lv.addHeaderView(addItem);
  75.  
  76.  
  77.  
  78.         fillData();
  79.  
  80. }
  81.  
  82.  
  83.  
  84.     private void fillData()
  85.  
  86.     {
  87.  
  88.         String[] items = new String[] { "Item A", "Item B", "Item C" };
  89.  
  90.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_row, R.id.TextView01, items) {
  91.  
  92.                 @Override
  93.  
  94.                 public View getView(int position, View convertView, ViewGroup parent) {
  95.  
  96.                         View v = super.getView(position, convertView, parent);
  97.  
  98.                         v.setOnClickListener(testListener);
  99.  
  100.                         v.setLongClickable(true);
  101.  
  102.                         return v;
  103.  
  104.                 }
  105.  
  106.         };
  107.  
  108.         setListAdapter(adapter);
  109.  
  110.     }
  111.  
  112.  
  113.  
  114.     @Override
  115.  
  116.     protected Dialog onCreateDialog(int id) {
  117.  
  118.         switch (id) {
  119.  
  120.         case DIALOG_TEST:
  121.  
  122.                 return new AlertDialog.Builder(this)
  123.  
  124.                         .setTitle("Hello!")
  125.  
  126.                         .setPositiveButton("Close", null)
  127.  
  128.                         .create();
  129.  
  130.         }
  131.  
  132.         return super.onCreateDialog(id);
  133.  
  134.     }
  135.  
  136.  
  137.  
  138.     @Override
  139.  
  140.     public void onCreateContextMenu(ContextMenu menu, View v,
  141.  
  142.                 ContextMenuInfo menuInfo) {
  143.  
  144.         super.onCreateContextMenu(menu, v, menuInfo);
  145.  
  146.         menu.add("Context item");
  147.  
  148.     }
  149.  
  150.  
  151.  
  152. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


layout/main.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.        android:layout_width="fill_parent"
  6.  
  7.    android:layout_height="fill_parent">
  8.  
  9.  
  10.  
  11.             <ListView android:id="@+id/android:list"
  12.  
  13.                android:layout_width="fill_parent"
  14.  
  15.                android:layout_height="fill_parent"
  16.  
  17.                android:focusable="false" />
  18.  
  19.             <include android:id="@+id/android:empty" layout="@layout/list_header" />
  20.  
  21. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


layout/list_header.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <!-- This seems way too complicated, but the outer view's layout params are apparently
  4.  
  5.        ignored, so I don't see a simpler way to (1) center the text vertically in a large
  6.  
  7.        row and (2) make the background highlight fill the entire row. -->
  8.  
  9. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  10.  
  11.        android:layout_width="wrap_content" android:layout_height="wrap_content"
  12.  
  13.        android:background="@android:drawable/menuitem_background"
  14.  
  15.        android:focusable="true" android:descendantFocusability="blocksDescendants">
  16.  
  17.         <FrameLayout android:layout_width="99999dip" android:layout_height="50dip">
  18.  
  19.                         <TextView android:text="List header view"
  20.  
  21.                                android:textAppearance="?android:attr/textAppearanceLarge"
  22.  
  23.                                android:layout_gravity="center_vertical"
  24.  
  25.                                android:layout_width="wrap_content" android:layout_height="wrap_content"
  26.  
  27.                                android:paddingLeft="10dip"></TextView>
  28.  
  29.         </FrameLayout>
  30.  
  31. </FrameLayout>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


layout/list_row.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <RelativeLayout android:id="@+id/RelativeLayout01"
  4.  
  5.        xmlns:android="http://schemas.android.com/apk/res/android" android:clickable="true"
  6.  
  7.        android:focusable="true" android:background="@drawable/android:menuitem_background"
  8.  
  9.        android:layout_width="fill_parent" android:layout_height="wrap_content"
  10.  
  11.        android:descendantFocusability="blocksDescendants">
  12.  
  13.                 <CheckBox android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
  14.  
  15.                        android:layout_width="wrap_content" android:layout_height="wrap_content"
  16.  
  17.                        android:id="@+id/CheckBox01" android:layout_marginTop="1dip"
  18.  
  19.                        android:layout_marginRight="2.5dip" android:layout_marginLeft="1dip"></CheckBox>
  20.  
  21.                 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
  22.  
  23.                        android:layout_alignParentTop="true" android:id="@+id/TextView01"
  24.  
  25.                        android:text="Top left" android:textStyle="bold"
  26.  
  27.                        android:layout_toRightOf="@+id/CheckBox01" android:layout_marginTop="5dip"
  28.  
  29.                        android:focusable="false"></TextView>
  30.  
  31.                 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
  32.  
  33.                        android:text="Bottom left" android:layout_alignLeft="@+id/TextView01"
  34.  
  35.                        android:id="@+id/TextView02" android:focusable="false"
  36.  
  37.                        android:layout_below="@+id/TextView01"></TextView>
  38.  
  39.                 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
  40.  
  41.                        android:text="Top right" android:layout_alignParentTop="true"
  42.  
  43.                        android:layout_alignParentRight="true" android:id="@+id/TextView03"
  44.  
  45.                        android:textStyle="bold" android:layout_marginRight="5dip"
  46.  
  47.                        android:layout_marginTop="5dip" android:focusable="false"></TextView>
  48.  
  49.                 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
  50.  
  51.                        android:text="Bottom right" android:id="@+id/TextView04"
  52.  
  53.                        android:layout_below="@+id/TextView03" android:layout_alignRight="@+id/TextView03"
  54.  
  55.                        android:focusable="false"></TextView>
  56.  
  57. </RelativeLayout>
Parsed in 0.007 seconds, using GeSHi 1.0.8.4
Attachments
DpadBug.zip
complete Eclipse project
(9.17 KiB) Downloaded 52 times
Mr2001
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Mar 08, 2009 11:48 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests