3

问题

我正在寻找一种效果,即最初按下一个按钮会将其他 3 个按钮的可见性从消失变为可见。在向一个新可见的按钮滑动并结束后,我想启动一个不同的活动。

我的问题是,无论我使用哪种方法来获取视图的位置(使用 Rect 对象,findNearestTouchable)和位置触摸事件(使用带有 ACTION_UP 的 onTouch/Gestures),它们似乎永远不会匹配,我永远无法返回我想要的视图;

所需的功能

从按下按钮开始并在另一个按钮上结束的动作事件,以返回它结束的按钮的 id/view 对象。

我的 main_menu.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button"        
        android:tag="1"

        />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_marginBottom="36dp"
        android:layout_marginRight="19dp"
        android:layout_toLeftOf="@+id/button1"
        android:text="Button"
        android:visibility="gone"
        android:tag="3" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="Button"
        android:visibility="gone" 
        android:tag="2"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:text="Button"
        android:visibility="gone"
        android:tag="4" />

</RelativeLayout>

当前对 Activity 的尝试,未设置 foundit(我对完全不同的实现非常开放)

public class Menu extends Activity {
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                View findViewById2 = findViewById(R.id.button2);
                View findViewById3 = findViewById(R.id.button3);
                View findViewById4 = findViewById(R.id.button4);
                findViewById2.setVisibility(View.VISIBLE);
                findViewById3.setVisibility(View.VISIBLE);
                findViewById4.setVisibility(View.VISIBLE);
                return gestureDetector.onTouchEvent(event);
            }
        };
        View button = findViewById(R.id.button1);
        button.setOnTouchListener(gestureListener);

    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {

            View foundit = null;
            ViewGroup menu = (ViewGroup) findViewById(R.id.button1).getParent();
            for (int numChildren = menu.getChildCount(); numChildren >= 0; --numChildren) {
                View child = menu.getChildAt(numChildren - 1);
                if (child instanceof Button) {
                    Rect bounds = new Rect();
                    child.getHitRect(bounds);
                    if (bounds.contains((int) e2.getX(), (int) e2.getY())) {
                        foundit = child;
                    }
                }
            }
            if (foundit != null) {
                Log.i("found", "FOUND IT");
            }
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }

}

有没有人有任何见解?

编辑

测试以下内容:

    gestureListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            View findViewById2 = findViewById(R.id.button2);
            View findViewById3 = findViewById(R.id.button3);
            View findViewById4 = findViewById(R.id.button4);
            findViewById2.setVisibility(View.VISIBLE);
            findViewById3.setVisibility(View.VISIBLE);
            findViewById4.setVisibility(View.VISIBLE);
            if (event.getAction() == MotionEvent.ACTION_UP) {
                Log.i("TOUCH X IS", Float.toString(event.getRawX()));
                Log.i("TOUCH Y IS", Float.toString(event.getRawY()));
            }
            for (int numChildren = ((ViewGroup) v.getParent())
                    .getChildCount(); numChildren >= 0; --numChildren) {
                View child = ((ViewGroup) v.getParent())
                        .getChildAt(numChildren - 1);
                if (child instanceof Button) {
                    Rect bounds = new Rect();
                    child.getHitRect(bounds);
                    Log.i(child.getTag().toString() + " CENTER X IS",
                            Float.toString(bounds.exactCenterX()));
                    Log.i(child.getTag().toString() + " CENTER Y IS",
                            Float.toString(bounds.exactCenterY()));
                }
            }
            return false;
        }
    };

产生以下 LogCat 输出:

07-06 01:12:58.245: TOUCH X IS(20965): 394.0

07-06 01:12:58.245: TOUCH Y IS(20965): 297.0

07-06 01:12:58.245: 4 CENTER X IS(20965): 392.0

07-06 01:12:58.245: 4 CENTER Y IS(20965): 219.0

07-06 01:12:58.245: 2 CENTER X IS(20965): 240.0

07-06 01:12:58.250: 2 CENTER Y IS(20965): 219.0

07-06 01:12:58.250: 3 CENTER X IS(20965): 95.0

07-06 01:12:58.250: 3 CENTER Y IS(20965): 219.0

07-06 01:12:58.250: 1 CENTER X IS(20965): 240.0

07-06 01:12:58.250: 1 CENTER Y IS(20965): 345.0

4

2 回答 2

1

获取子视图,如果它在线性布局或网格布局中可以通过下面的方法。

|*| 如果视图垂直放置在 LinearLayout 中,我已经向你展示了如何获取 SubView。

NamVyuVar.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View view, MotionEvent MsnEvtPsgVar)
    {
        switch (MsnEvtPsgVar.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                Log.d("TAG", "onTouch: ACTION_DOWN");
                break;

            case MotionEvent.ACTION_MOVE:
                LinearLayout NamLloVav = (LinearLayout)view;
                float SubVyuHytVal = NamLloVav.getChildAt(0).getHeight();
                float TchPntYcoVal = MsnEvtPsgVar.getY();
                int NamIdxVal = (int) (TchPntYcoVal / SubVyuHytVal);

                View NamIdxVyuVav = NamLloVav.getChildAt(NamIdxVal)

            // CodTodo With the Indexed View NamIdxVyuVav :

                break;

            case MotionEvent.ACTION_UP:
                Log.d("TAG", "onTouch: ACTION_UP");
                break;
        }
        return true;
    }
});

|*| 同样,如果您想获得子视图,如果视图水平放置在 LinearLayout 内,则更改为 3 行以下。

float SubVyuWdtVal = NamLloVav.getChildAt(0).getWidth();
float TchPntXcoVal = MsnEvtPsgVar.getX();
int NamIdxVal = (int) (TchPntXcoVal / SubVyuWdtVal);

|*| 同样的技术也可以应用于网格布局。

于 2017-05-20T07:47:28.110 回答
0

在您的手势监听器中,检查 MotionEvent 何时为 MotionEvent.ACTION_UP 并使用坐标 event.getX() 和 event.getY(),然后确定在这些坐标处的视图。

public boolean onTouch(View view, MotionEvent event) {
  if (event == MotionEvent.ACTION_UP) {
    int x = event.getX();
    int y = event.getY();
    //find what view is at x, y
  }
}

当按下第一个按钮时(使用 onClick 或当运动事件为 ACTION_DOWN 时),您将触发一些标志。然后,当他们抬起手指时,上面的内容就会被解雇。

编辑:一定要使用 event.getY() 而不是 event.getRawY()。计算方式不同,按钮边界的计算方式与 getY() 相同

于 2012-07-05T23:53:15.267 回答