0

リニアレイアウトのタッチイベントで複数の画面がポップアップします。線形レイアウトの子要素ごとに ontouch イベントが 2 回発生するようです。これが2回発火するのを防ぐにはどうすればよいですか。同じリニア レイアウトのクリック イベントでは、リニア レイアウトの onclick イベントが最初のクリックではなく 2 回目のクリックで発生します。どこが間違っているのか理解できません。助けてください。

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

-- Oncreate メソッドのアクティビティ

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 リスナー

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

3 に答える 3

0

問題は、タッチのアクション MOVE、UP、および DOWN を定義していないためだと思います。MotionEvent.ACTION_DOWNにコードを書いてみてください。これがうまくいく場合。

    case R.id.layout_button_subscriptions:{   
      switch (event.getAction()) {
                        case MotionEvent.ACTION_MOVE: 
                              {

                                 break;
                              }
                        case MotionEvent.ACTION_DOWN: {

                            startActivity(new Intent(AppActivity.this,Subs.class));
                        break;
                    }
    }
于 2013-10-18T05:41:32.083 に答える
0

私が見ることができるコードからの最初の間違いは、次のようにする代わりに、タッチリスナーを2回設定していることです

mOnTouchListener listener =  new mOnTouchListener();
linearLayoutSubs.setOnTouchListener(listener);
linearLayoutFind.setOnTouchListener(listener);

可能であれば、onTouch で if else 条件を使用し、onTouch が発生するたびに、その位置からのみ true を返します。

多くの場合、その位置から戻らないため、イベントが 2 回発生します。

于 2013-10-18T05:57:09.653 に答える