我有一个触控侦听器,可以侦听视图以向左/向右滑动
我传递视图(这会将整个listview项目传递给方法)然后我需要抓住我想要滑动的特定元素(叠加层)
这是触摸事件
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
{
this._downX = e.GetX();
this._downY = e.GetY();
return true;
}
case MotionEventActions.Up:
{
this._upX = e.GetX();
this._upY = e.GetY();
float deltaX = this._downX - this._upX;
float deltaY = this._downY - this._upY;
// swipe horizontal?
if (Math.Abs(deltaX) > MIN_DISTANCE)
{
//left or right
if (deltaX < 0)
{
this.onLeftToRightSwipe(v);
return true;
}
if (deltaX > 0)
{
this.onRightToLeftSwipe(v);
return true;
}
}
return false;
}
}
return false;
}
以下是slideView返回Null的幻灯片方法
public void onRightToLeftSwipe(View v)
{
View view = v;
Animation slideLeft = AnimationUtils.LoadAnimation(this._activity, Resource.Animation.slide_left);
View sliderView = v.FindViewById(Resource.Id.list_view_item_parent); //THIS RETURNS NULL
sliderView.StartAnimation(slideLeft);
}
这是布局的一部分,其中v是RelativeLayout,我试图抓住list_view_item_parent
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right">
<Button
android:text="Adjust"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Move"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android.id="@+id/list_view_item_parent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000">
任何想法会发生什么?
答案 0 :(得分:0)
现在我已经使用了一个围绕所有孩子骑自行车的工作只有一个孩子,因为相对布局中的另一个视图有3个孩子......它很脏,但它适用于这种情况。仍在寻找更好的答案。
public void OnRightToLeftSwipe(View v)
{
View view = v;
Animation slideLeft = AnimationUtils.LoadAnimation(this._activity, Resource.Animation.slide_left);
View sliderView = null;
int count = ((RelativeLayout)v).ChildCount;
for(int i = 0; i < count; i++)
{
View v2 = ((RelativeLayout)v).GetChildAt(i);
if (((LinearLayout)v2).ChildCount == 1)
{
sliderView = v2;
break;
}
}
//View sliderView = v.FindViewById(Resource.Id.list_view_item_parent);
sliderView.StartAnimation(slideLeft);
}