简体   繁体   English

如何在LinearLayout上添加ontouchlistener或clicklistener?

[英]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? 我在linearlayout上添加了一个ontouchlistener,其中包括一个TextView和一个ImageView。当我触摸TextView部分时,它起作用了,而ImageView不起作用。我希望所有linearlayout都可以得到touch事件。我该怎么办? My XML is as follows: 我的XML如下:

<?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 . 使用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 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 在此侦听器中,不要调用晚饭,并返回false,这样事件将传播到下一个侦听器,希望是布局

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 另外,在图像的侦听器的ontouch函数中设置一个断点,以查看是否到达该位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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