1

注:同様の問題に直面しているためにここにいる場合は、提供されているすべての回答を読んで試してください。状況によっては、ある環境が別の環境よりもうまく機能するように見えました。だからあなた自身のテストもやってください!

序文

私はこのようなリストビューの行レイアウトを取得しました(これは私の実際のレイアウトのストリップダウンバージョンなので、LinearLayoutなどでラップされた単一要素などについてはコメントしないでください-問題を説明するためだけです-最終バージョンにはより多くの要素があります):

ここに画像の説明を入力

参照用に XML ファイルを以下に示しますが、達成したいことを簡単に説明します。

  • 青いボックス (アイコン) と「通常はここに長いテキスト」TextView は行の「実際の」コンテンツ ( item_main_container) です。それは何でもかまいません - それは無関係です。このレイヤー コンテンツはクリックできません。実際(しかし、私は気づいたのですが)次のレイヤーから、「青いボックス」の半分が赤で覆われ、半分が緑のボックスで覆われていることを示す必要があります。
  • 赤と緑のボックスは、行の OnCliclListeners をバインドするビューです ( にラップされていitem_click_area_containerます)- のコンテンツが実際に何であるかに関係なく、ユーザーが行をタップできるようにしたいだけですitem_main_container(したがって、これらの赤/緑はアプリで透明です) . 注:これらはイメージ上で透明に設定されているため、透けて見ることができますitem_main_contentが、これはデモンストレーションのみを目的としています。また、赤と緑は別々item_click_area_containerのアクションを表していることに注意してください。ユーザーがタップした場所に応じてより多くのアクションを処理できるようにするには、レイヤー上により多くの要素を配置できる必要があります。
  • 黄色の領域 ( button_bar_container) には、ユーザーがタップする可能性のあるボタンがあります。

したがって、基本的にはitem_click_area_containerオーバーレイですitem_main_containerが、button_bar_container上にある ではありません。item_click_area_containerそうしないと、ユーザーはそのボタンをタップできません。概観すると、次のようになります。

ここに画像の説明を入力

レイアウトの概要は次のようになります。

ここに画像の説明を入力


問題

私の努力にもかかわらず、全体item_click_area_containerをオーバーレイするために正しくストレッチすることができません。テキストは任意の長さにできるため、 の高さがわかりません。以下の XMLには、すべての行要素が表示されるという点で機能しますが、テキストが長い場合、高さが小さすぎます。しかし、それを置き換えると、全体が見えなくなり、目的が果たせなくなります。 item_main_container"usually long text here"item_main_containerandroid:layout_height="100dpitem_click_area_containerandroid:layout_height="fill_parent"item_click_area_container


質問

  1. これが期待どおりに機能しないのはなぜですか、および/またはそれを修正する方法は?
  2. 私のようなアプローチを使用するよりも、コンテンツに依存しないクリック領域を持つより良い方法はありますかitem_click_area_container(ただし、OnTouchListener ベースのソリューションはありません)。

レイアウト XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent" 
          android:layout_height="wrap_content">

        <LinearLayout 
            android:id="@+id/item_main_container"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content">

            <ImageView 
               android:id="@+id/item_avatar" 
               android:layout_width="40dp" 
               android:layout_height="40dp" 
               android:background="#0000ff" 
               android:src="@drawable/icon_help"/>

            <LinearLayout 
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content" 
               android:orientation="vertical">

               <TextView android:id="@+id/item_body" 
                 android:layout_width="fill_parent" 
                 android:layout_height="fill_parent" 
                 android:text="Usually long text here..." 
                 android:textColor="#ffffff"/>
            </LinearLayout>

        </LinearLayout>

        <LinearLayout 
               android:id="@+id/item_click_area_container" 
               android:layout_width="fill_parent" 
               android:layout_height="100dp">
            <LinearLayout 
               android:id="@+id/green_click" 
               android:layout_width="fill_parent" 
               android:layout_height="fill_parent" 
               android:layout_weight="1" 
               android:background="#7700ff00">
            </LinearLayout>
            <LinearLayout 
               android:id="@+id/red_click" 
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_weight="2" 
               android:background="#77ff0000">
            </LinearLayout>
        </LinearLayout>

    </RelativeLayout>

    <LinearLayout 
       android:id="@+id/button_bar_container" 
       android:layout_width="fill_parent" 
       android:layout_height="50dp" 
       android:layout_marginTop="3dp" 
       android:background="#ffff00">
    </LinearLayout>

</LinearLayout>

編集#1

私はOnTouchListener, and in onTouch()checkevent.getAction()を使用MotionEvent.ACTION_DOWNして、イベントの X/Y を読み取り、ビューの境界に対してチェックすることができると思います。これでうまくいくかもしれませんが、可能であれば OnClickListener だけに固執したいと思います。言うまでもなく、現在の犯人が何であるかを理解するのに役立ちます。特に、ADT でレイアウトが正しくレンダリングされます。

4

3 に答える 3

3

ほら、このレイアウトが機能します。簡単な解決策は、次の 4 つの線item_click_area_containerを使用して、 の 4 辺を の対応する 4 辺に揃えることです。item_main_container

        android:layout_alignTop="@+id/item_main_container"
        android:layout_alignBottom="@+id/item_main_container"
        android:layout_alignLeft="@+id/item_main_container"
        android:layout_alignRight="@+id/item_main_container"

これがwrap_contentの高さのレイアウトです。ハードコーディングされたサイズはもうありません (:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/item_main_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/item_avatar"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="#0000ff"
                android:src="@drawable/ic_action_search" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/item_body"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="Usually long text hasdkfn ash jsjkgh sjhgjk hasjkh ash fjk hasdjkh fkjash jkdf hjkasdh fjkhasjkg hjkasjkd hfjkash gsdgh kgh sdfh fjksah djkfasd jkg jsdf asgjf sg sd agfgas fjhasg f asdgf gjhsdg sdjhsdg fasdfjhs agjhfg  adf asdg jh dvhsdvjhas gdh fv gsfjivhdfvjksfgjkdhjhixcgzvjk gdshg dfg v gvgdfgvjkshvjksdgvsdvere..."
                    android:textColor="#000" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_alignTop="@+id/item_main_container"
            android:layout_alignBottom="@+id/item_main_container"
            android:layout_alignLeft="@+id/item_main_container"
            android:layout_alignRight="@+id/item_main_container"
            android:id="@+id/item_click_area_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/green_click"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#7700ff00" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/red_click"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="2"
                android:background="#77ff0000" >
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/button_bar_container"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginTop="3dp"
        android:background="#ffff00" >
    </LinearLayout>

</LinearLayout>
于 2012-11-02T19:22:12.747 に答える
2

あなたはあなたを置き換えてみることができます

<RelativeLayout
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">...</RelativeLayout>

とともに

<FrameLayout
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">...</FrameLayout>

次に、パラメータitem_click_area_containerfill_parent/match_parentに設定して、 と同じ領域をカバーするように成長させますitem_main_container

LinearLayout赤と緑の領域に必要なアクションを処理する拡張クラスを使用する場合は、問題ありません。

于 2012-10-31T03:50:50.423 に答える
0

このようなことができます。

layout.xml android:onClick の個々の要素について

    <LinearLayout 
       android:id="@+id/red_click" 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="2" 
       android:background="#77ff0000">
   android:onClick="redAreaClicked"
    </LinearLayout>

Java ファイルに次のコードでメソッド redAreaClicked() を記述します。

    public void redAreaClicked(View v) {
      RelativeLayout ParentRow = (RelativeLayout) v.getParent();
      final int position = mList.getPositionForView(ParentRow);
      // more code here..
    }

これにより、リスト内の位置が表示されます。つまり、行番号です。ただし、これを機能させるには、すべての子ビュー (この場合は線形レイアウトのカラー ビュー) を 1 つの親内に移動する必要があります。相対レイアウトを使用すると、それは非常に実現可能だと思います。

または、親ビューを取得するために、view.getId() を使用して比較することもできます。次に、リスト内の位置を決定します。

他の色の領域についても同じことができます。

于 2012-10-31T04:16:06.137 に答える