【问题标题】:Handling click events in GridView cell with Spinner使用 Spinner 处理 GridView 单元格中的单击事件
【发布时间】:2013-06-24 02:40:50
【问题描述】:

我使用以下代码来捕获对 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 对象,但包含的 gridview 单元格不再响应单击。我假设 Spinner 侦听器正在替换 gridview 侦听器。 gridview 和 spinner listeners 单独工作都很好,但在一起我有问题。我需要能够同时点击两者。以下是微调器代码:

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>

提前谢谢你。

【问题讨论】:

  • 如果您无法弄清楚这一点,您可能想尝试一种丑陋的方法。为 Spinner 创建一个 onItemClickListener,它只是调用 GridView 项的 onItemClickListener。
  • 单击一个单元格通常会打开另一个屏幕,其中包含详细信息(比单元格视图中提供的几行更多)。微调器只是最常用功能的快捷方式(他们从列表中选择一个名称并对其采取行动)。如果他们采取该操作,则无需打开附加视图,因为工作已完成。如果我将微调器拿走并将其放入另一个屏幕(详细视图),那么我可能会遇到另一个问题,因为该屏幕还包含一个侦听器。
  • 这似乎是一个常见问题,但我没有找到与此问题相关的线程。
  • 好的。解决了。我不得不拼凑它。我向 gridview 单元格中包含的每个 textview 添加了一个 OnClickListener,并从 gridview 本身中删除了侦听器。现在可以了。不知道为什么其他方法不起作用但不能花很多时间。

标签: android xml gridview spinner


【解决方案1】:

您需要在相对布局上调用“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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    相关资源
    最近更新 更多