问题
我正在寻找一种效果,即最初按下一个按钮会将其他 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