本文介绍了如何prevent onTouch父线性布局从执行多次的孩子呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在触摸事件的lienar布局的多个屏幕上弹出。看来,ontouch事件获取的线性布局的每个子元素触发了两次。我怎样才能prevent这种烧制而成的两倍。在单击事件相同的线性布局的原因,线性布局onclick事件对第二次点击,而不是第一次点击被解雇。我不理解我要去的地方错了。请大家帮帮忙。

- XML
    

 <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:重力=center_vertical
        机器人:方向=垂直>

        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=center_horizo​​ntal
            机器人:方向=横向
            机器人:weightSum =1.0>

            <的LinearLayout
                机器人:ID =@ + ID / layout_button_sub
                机器人:layout_width =0dp
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =@扪/ Home.Item.Spacing.Vertical
                机器人:layout_marginLeft =@扪/ Home.Item.Spacing.Horizo​​ntal
                机器人:layout_marginRight =@扪/ Home.Item.Spacing.Horizo​​ntal
                机器人:layout_marginTop =@扪/ Home.Item.Spacing.Vertical
                机器人:layout_weight =0.5
                机器人:背景=@可绘制/ bg_home_item
                机器人:可点击=真
                机器人:descendantFocusability =blocksDescendants
                机器人:可聚焦=真
                机器人:focusableInTouchMode =真
                机器人:inputType =无>

                    < ImageView的
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:duplicateParentState =假
                        机器人:focusableInTouchMode =假
                        机器人:SRC =@可绘制/ ic_subs/>

                    <的TextView
                        机器人:layout_width =FILL_PARENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:duplicateParentState =假
                        机器人:focusableInTouchMode =假
                        机器人:重力=右
                        机器人:inputType =无
                        机器人:文本=预订
                        机器人:文字颜色=@色/白
                        机器人:TEXTSIZE =@扪/ Normal.Text.Size/>
            < / LinearLayout中>

            <的LinearLayout
                机器人:ID =@ + ID / layout_button_find
                机器人:layout_width =0dp
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =@扪/ Home.Item.Spacing.Vertical
                机器人:layout_marginLeft =@扪/ Home.Item.Spacing.Horizo​​ntal
                机器人:layout_marginRight =@扪/ Home.Item.Spacing.Horizo​​ntal
                机器人:layout_marginTop =@扪/ Home.Item.Spacing.Vertical
                机器人:layout_weight =0.5
                机器人:背景=@可绘制/ bg_home_item
                机器人:可点击=真
                机器人:descendantFocusability =blocksDescendants
                机器人:可聚焦=真
                机器人:focusableInTouchMode =真
                机器人:inputType =无>

                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:duplicateParentState =假
                    机器人:focusableInTouchMode =假
                    机器人:SRC =@可绘制/ ic_find/>

                <的TextView
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:duplicateParentState =假
                    机器人:focusableInTouchMode =假
                    机器人:重力=右
                    机器人:inputType =无
                    机器人:文本=查找附近的
                    机器人:文字颜色=@色/白
                    机器人:TEXTSIZE =@扪/ Normal.Text.Size/>
            < / LinearLayout中>
        < / LinearLayout中>
    < / LinearLayout中>
< /滚动型>
 

- 在onCreate方法的活动

 公共无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
    的setContentView(R.layout.layout_main);
    linearLayoutSubs =(的LinearLayout)findViewById(R.id.layout_button_sub);
    linearLayoutFind =(的LinearLayout)findViewById(R.id.layout_button_find);

    linearLayoutSubs.setOnTouchListener(新mOnTouchListener());
    linearLayoutFind.setOnTouchListener(新mOnTouchListener());
}
 

- onTouch监听

 公共类mOnTouchListener实现OnTouchListener {
    意向意图= NULL;
    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        开关(v.getId()){
            案例R.id.layout_button_subscriptions:
                意图=新的意图(AppActivity.this,Subs.class); //类导航
                打破;
            案例R.id.layout_button_find_nearby:
                意图=新的意图(AppActivity.this,Find.class); //类导航
                打破;
        }

        如果(意向!= NULL){
            startActivity(意向);
        }
        返回true;
    }
}
 

解决方案

首先,删除机器人:可调焦=真正的安卓focusableInTouchMode =你的布局XML真正的 layout_button_sub layout_button_find 。这prevents第一触摸/点击事件被消耗的一个焦点变化。

然后,改变你的 OnTouchListener OnClickListener

 私有类mOnClickListener实现OnClickListener
{
    意向意图;

    公共无效的onClick(视图v)
    {
       开关(v.getId())
       {
           案例R.id.layout_button_sub:
                意图=新的意图(AppActivity.this,Subs.class);
                打破;
           案例R.id.layout_button_find:
                意图=新的意图(AppActivity.this,Find.class);
                打破;
        }

        如果(意向!= NULL)
        {
            startActivity(意向);
        }
    }
}
 

并将其设置为这样的你LinearLayouts:

  mOnClickListener监听器=新mOnClickListener();
linearLayoutSubs.setOnClickListener(听众);
linearLayoutFind.setOnClickListener(听众);
 

onTouch()若干MotionEvents事件触发: ACTION_DOWN ACTION_MOVE ,等你点击与每次 OnTouchListener ,该方法烧制,以最低的,一旦当你把你的手指下,一旦当你抬起你的手指。由于原来的 onTouch()方法没有考虑不同的行动, startActivity()正在呼吁每触摸事件。

On touch event for a lienar layout multiple screens pops up. It seems that the ontouch event gets fired twice for each child element of the linear layout. How can I prevent this from firing twice. On click event for the same linear layout causes, the linear layout onclick event to be fired on second click and not on the first click. I am not understanding where I am going wrong. Please help.

-- XML

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            android:weightSum="1.0" >

            <LinearLayout
                android:id="@+id/layout_button_sub"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/Home.Item.Spacing.Vertical"
                android:layout_marginLeft="@dimen/Home.Item.Spacing.Horizontal"
                android:layout_marginRight="@dimen/Home.Item.Spacing.Horizontal"
                android:layout_marginTop="@dimen/Home.Item.Spacing.Vertical"
                android:layout_weight="0.5"
                android:background="@drawable/bg_home_item"
                android:clickable="true"
                android:descendantFocusability="blocksDescendants"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:inputType="none" >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:duplicateParentState="false"
                        android:focusableInTouchMode="false"
                        android:src="@drawable/ic_subs" />

                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:duplicateParentState="false"
                        android:focusableInTouchMode="false"
                        android:gravity="right"
                        android:inputType="none"
                        android:text="Subscriptions"
                        android:textColor="@color/White"
                        android:textSize="@dimen/Normal.Text.Size" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout_button_find"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/Home.Item.Spacing.Vertical"
                android:layout_marginLeft="@dimen/Home.Item.Spacing.Horizontal"
                android:layout_marginRight="@dimen/Home.Item.Spacing.Horizontal"
                android:layout_marginTop="@dimen/Home.Item.Spacing.Vertical"
                android:layout_weight="0.5"
                android:background="@drawable/bg_home_item"
                android:clickable="true"
                android:descendantFocusability="blocksDescendants"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:inputType="none">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:duplicateParentState="false"
                    android:focusableInTouchMode="false"
                    android:src="@drawable/ic_find" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:duplicateParentState="false"
                    android:focusableInTouchMode="false"
                    android:gravity="right"
                    android:inputType="none"
                    android:text="Find Nearby"
                    android:textColor="@color/White"
                    android:textSize="@dimen/Normal.Text.Size" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

-- Activity on Oncreate method

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);
    linearLayoutSubs = (LinearLayout)findViewById(R.id.layout_button_sub);
    linearLayoutFind = (LinearLayout)findViewById(R.id.layout_button_find);

    linearLayoutSubs.setOnTouchListener(new mOnTouchListener());
    linearLayoutFind.setOnTouchListener(new mOnTouchListener());
}

-- onTouch listener

public class  mOnTouchListener implements OnTouchListener {
    Intent intent = null;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(v.getId()) {
            case R.id.layout_button_subscriptions:
                intent = new Intent(AppActivity.this, Subs.class);//class for navigation
                break;
            case R.id.layout_button_find_nearby:
                intent = new Intent(AppActivity.this, Find.class);//class for navigation
                break;
        }

        if(intent != null) {
            startActivity(intent);
        }
        return true;
    }
}
解决方案

First, remove android:focusable="true" and android:focusableInTouchMode="true" from layout_button_sub and layout_button_find in your layout xml.This prevents the first touch/click event from being consumed by a focus change.

Then, change your OnTouchListener to an OnClickListener:

private class mOnClickListener implements OnClickListener
{
    Intent intent;

    public void onClick(View v)
    {
       switch (v.getId()) 
       {
           case R.id.layout_button_sub: 
                intent = new Intent(AppActivity.this, Subs.class);
                break;
           case R.id.layout_button_find: 
                intent = new Intent(AppActivity.this, Find.class);
                break; 
        }

        if (intent != null) 
        {
            startActivity(intent); 
        } 
    }       
}

And set it as such for your LinearLayouts:

mOnClickListener listener = new mOnClickListener();
linearLayoutSubs.setOnClickListener(listener);
linearLayoutFind.setOnClickListener(listener);

The onTouch() event fires on several MotionEvents: ACTION_DOWN, ACTION_MOVE, etc. Every time you "clicked" with an OnTouchListener, the method was firing, at minimum, once when you put your finger down and once when you lifted your finger. Since your original onTouch() method didn't account for the different actions, startActivity() was being called for every touch event.

这篇关于如何prevent onTouch父线性布局从执行多次的孩子呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:25