我已经设置了一个 GridView 来显示产品列表。网格工作正常。当我使用单个 relativeLayout 时,效果很好。但是,当我在项目 XML 中添加另一个 RelativeLayout 以包含其他详细信息时,将不会调用单击侦听器。
网格与我的点击监听器一起定义如下:
gridview = (GridView) findViewById(R.id.browse_devices);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(mContext,ShowProductDetail.class);
Bundle bundle = new Bundle();
bundle.putString("SKU", skuList.get(position));
intent.putExtras(bundle);
startActivity(intent);
}
});
我的项目描述如果我只有标题、几个字幕和一个产品图片,点击监听器工作正常,点击一个产品会导致 ShowProductDetail 活动开始。该定义是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/off_white" >
<TextView
android:id="@+id/br_product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="4dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/devil_gray" />
<TextView
android:id="@+id/br_product_subtitle0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/br_product_title"
android:layout_marginTop="8dp"
android:text="TextView"
android:textColor="@color/devil_gray" />
<TextView
android:id="@+id/br_product_subtitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/br_product_subtitle0"
android:layout_marginTop="4dp"
android:text="TextView"
android:textColor="@color/devil_gray" />
<TextView
android:id="@+id/br_product_subtitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/br_product_subtitle1"
android:layout_marginTop="4dp"
android:text="TextView" />
<ImageView
android:id="@+id/separator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/br_product_title"
android:src="@drawable/separator_black" />
<ImageView
android:id="@+id/br_product_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" />
看起来像这样:
但是当我添加以下 XML 以使用户能够选择内存、颜色和数量时,永远不会调用单击侦听器。这是我添加到底部的附加 RelativeLayout:
<RelativeLayout
android:id="@+id/details_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/br_product_image"
android:layout_marginTop="5dp"
android:background="@color/off_white" >
<TextView
android:id="@+id/capacity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="false"
android:text="@string/device_mem" />
<TextView
android:id="@+id/color_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/capacity_title"
android:text="@string/device_color" />
<TextView
android:id="@+id/quantity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/color_title"
android:text="@string/quantity" />
<Spinner
android:id="@+id/memory_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/capacity_title"
android:layout_alignStart="@id/capacity_title"
android:layout_below="@id/capacity_title" />
<Spinner
android:id="@+id/color_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/color_title"
android:layout_alignStart="@id/color_title"
android:layout_below="@+id/color_title" />
<Spinner
android:id="@+id/quantity_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/quantity_title"
android:layout_alignStart="@id/quantity_title"
android:layout_below="@+id/quantity_title" />
<TextView
android:id="@+id/device_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="false"
android:layout_below="@id/memory_spinner"
android:text="$199.99"
android:textSize="30sp" />
<Button
android:id="@+id/buy_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/quantity_spinner"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/device_price"
android:background="@drawable/btn_buy_now" />
</RelativeLayout>
应该是这样的:
任何人都可以通过向 GridView 中的项目添加第二个 RelativeLayout 来提供有关为什么无法调用单击侦听器的任何信息吗?