0

製品のリストを表示するように 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 の項目に 2 番目の RelativeLayout を追加するだけでクリック リスナーが呼び出されない理由について、誰でも情報を提供できますか?

4

1 に答える 1

0

両方の行の各子に同じ重みで水平に 2 つの LinearLayouts を使用することをお勧めします。これは、すべてのデバイスで普遍的に機能します。GridView は、方向の変更や小さいデバイスでは不均衡な列幅になります。

于 2013-07-02T16:45:52.047 に答える