使用Spinner处理GridView单元格中的单击事件



我使用以下代码捕捉网格视图项单元格上的单击。这很好。

grid_main.setAdapter(new ImageAdapter(this));
grid_main.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    }
});

然后,我在gridview单元格中添加了一个Spinner对象。现在,我可以点击Spinner对象,但包含网格视图的单元格不再响应点击。我假设Spinner监听器正在取代gridview监听器。gridview和spinner的听众单独工作都很好,但我一起有问题。我需要能够点击这两个。下面是微调器代码:

final Spinner spinner = (Spinner) v.findViewById(R.id.driver_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DispatchMonitorCalls.this,
android.R.layout.simple_spinner_item, driverlist_name);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    public void onItemSelected(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
    }
@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
} 
});

下面是布局xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/driverrow"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="left"
    android:gravity="left" >
    <TextView
        android:id="@+id/row_locationtype"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dip"
        android:layout_alignParentTop="true"        
        android:text="Empty"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/row_timestamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/row_locationtype"        
        android:layout_below="@+id/row_locationtype"
        android:text="Empty"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    <TextView
        android:id="@+id/row_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/row_locationtype"        
        android:layout_below="@+id/row_timestamp"
        android:text="Empty"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#ff8080" />
    <Spinner
        android:id="@+id/driver_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/row_locationtype"        
        android:layout_below="@+id/row_status"
        android:paddingBottom="20dp" />
</RelativeLayout>

提前谢谢。

您需要在相对布局上调用"blocksDecendants",因为微调器是可点击的,它会偷走您视图的焦点,因此您的"点击事件"会被带走

试试这个:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/driverrow"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="left"
    android:descendantFocusability="blocksDescendants" //<-- add this line, and your click event will be responded 
android:gravity="left" >
<TextView
    android:id="@+id/row_locationtype"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="10dip"
    android:layout_alignParentTop="true"        
    android:text="Empty"
    android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
    android:id="@+id/row_timestamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/row_locationtype"        
    android:layout_below="@+id/row_locationtype"
    android:text="Empty"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
    android:id="@+id/row_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/row_locationtype"        
    android:layout_below="@+id/row_timestamp"
    android:text="Empty"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#ff8080" />
<Spinner
    android:id="@+id/driver_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/row_locationtype"        
    android:layout_below="@+id/row_status"
    android:paddingBottom="20dp" />

最新更新