简体   繁体   中英

How to add a ontouchlistener or clicklistener on LinearLayout?

I added a ontouchlistener on linearlayout which include a TextView and a ImageView.When I touched the TextView part it works,and the ImageView doesn't work.I want all the linearlayout can get the touch event.What should I do? My XML is as follows:

<?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>

And this is my code:

dingyueLL.setOnTouchListener(new OnTouchListener()
    {

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

    });

Create custom linear layout with 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;    
    }
}

check this thread.

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

adding touch listener for liner layout filled with buttons

What i would try is to explicitly add a touch listener to the image, and see what happend. in this listener, dont call the supper, and return false, so the event will propagate to the next listener, hopefully the layout

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;
        }

    });

also, put a breakpoint in the ontouch function of the listener of the image, to see if it is getting there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM