2014-01-20 2 views
2

Я добавил ontouchlistener на linearlayout, который включает TextView и ImageView. Когда я коснулся части TextView, он работает, и ImageView не работает. Я хочу, чтобы все linearlayout могли получить событие touch. Что делать? Мой XML выглядит следующим образом:Как добавить ontouchlistener или clicklistener в LinearLayout?

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dip" 
    android:background="@drawable/graytitle_bj1_black" 
    android:gravity="center|bottom" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:id="@+id/nav_boutique" 
     android:layout_width="120dip" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_marginBottom="2dip" 
     android:clickable="true" 
     android:focusableInTouchMode="true" 
     android:descendantFocusability="blocksDescendants"> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/description" 
      android:src="@drawable/btn_jingpin_icon" 
      android:background="#FFF" 
      android:focusable="false" 
      android:clickable="true"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:textColor="#ffffff" 
      android:text="@string/bottom_jingpin"/> 
     <View 
      android:id="@+id/nav_boutique_line" 
      android:layout_width="fill_parent" 
      android:layout_height="2dip" 
      android:background="#F00"/> 
    </LinearLayout> 
    <View 
     android:layout_width="1dip" 
     android:layout_height="fill_parent"/> 
    <LinearLayout 
     android:id="@+id/nav_fenlei" 
     android:layout_width="120dip" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_marginBottom="2dip" 
     android:clickable="true" 
     android:descendantFocusability="blocksDescendants" 
     android:focusableInTouchMode="true"> 

     <ImageView    
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/description" 
      android:src="@drawable/icon_fenlei" 
      android:background="#00000000" 
      android:focusable="false" 
      android:clickable="true"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:textColor="#ffffff" 
      android:text="@string/bottom_fenlei"/> 
     <View 
      android:id="@+id/nav_fenlei_line" 
      android:layout_width="fill_parent" 
      android:layout_height="2dip"/> 
    </LinearLayout> 

    <View 
     android:layout_width="1dip" 
     android:layout_height="fill_parent"/> 
    <LinearLayout 
     android:id="@+id/nav_dingyue" 
     android:layout_width="120dip" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_marginBottom="2dip" 
     android:clickable="true" 
     android:descendantFocusability="blocksDescendants" 
     android:focusableInTouchMode="true"> 

     <ImageView    
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/description" 
      android:src="@drawable/btn_zizhudingyue_icon" 
      android:background="#00000000" 
      android:clickable="true" 
      android:focusable="false"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:textColor="#ffffff" 
      android:text="@string/bottom_dingyue"/> 
     <View 
      android:id="@+id/nav_dingyue_line" 
      android:layout_width="fill_parent" 
      android:layout_height="2dip"/> 
    </LinearLayout> 

    <View 
     android:layout_width="1dip" 
     android:layout_height="fill_parent"/> 
    <LinearLayout 
     android:id="@+id/nav_sousuo" 
     android:layout_width="120dip" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_marginBottom="2dip" 
     android:clickable="true" 
     android:descendantFocusability="blocksDescendants" 
     android:focusableInTouchMode="true"> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/description" 
      android:src="@drawable/icon_sousuo" 
      android:background="#00000000" 
      android:focusable="false" 
      android:clickable="true"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:textColor="#ffffff" 
      android:text="@string/bottom_sousuo"/> 
     <View 
      android:id="@+id/nav_sousuo_line" 
      android:layout_width="fill_parent" 
      android:layout_height="2dip"/> 
    </LinearLayout> 

</LinearLayout> 

И это мой код:

dingyueLL.setOnTouchListener(new OnTouchListener() 
    { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      setFlagTrue(Navigation.Dingyue.getPosition()); 
      setBtnLine(); 
      return false; 
     } 

    }); 
+1

вы говорите, «Мой XML будет следующим образом:» но вы вратаря запостить –

+0

Не уверен, почему он не работает для ImageView (нужно больше информации), но в то же время, ваш метод 'onTouch()', вероятно, должен возвращать 'true'. – Turix

+0

Вы отказались от своего собственного сообщения? Я перевернул его обратно –

ответ

3

Создайте пользовательский линейный макет с помощью onintercepttouchevent.

public class MyLayout extends LinearLayout { 
    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     // do whatever you want with the event 
     // and return true so that children don't receive it 
     return true;  
    } 
} 

проверить эту тему.

http://developer.android.com/training/gestures/viewgroup.html

adding touch listener for liner layout filled with buttons

+0

Спасибо. Это действительно полезно. –

+0

Ты так прав! на самом деле я видел это несколько дней назад, и я не помню, но это элегантное решение! +1 –

1

Что я хотел бы попробовать это явно добавить сенсорный слушателя к изображению, и посмотреть, что случилось. в этом слушателе, то не называют ужин, и вернуться ложным, поэтому событие будет распространяться на следующий слушатель, надеюсь макет

dingyueLL.setOnTouchListener(new OnTouchListener() 
    { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      setFlagTrue(Navigation.Dingyue.getPosition()); 
      setBtnLine(); 
      return false; 
     } 

    }); 

THEIMAGE.setOnTouchListener(new OnTouchListener() 
    { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 

      return false; 
     } 

    }); 

также, поставить точку останова в ontouch функции слушателя изображений, чтобы см., если он туда добирается

Смежные вопросы