我在如下所示的Listener
上创建了一个手势Activity
,效果很好:
[Activity (Theme = "@android:style/Theme.Holo.Light", Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/ic_launcher", ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class MainActivity : Activity, GestureDetector.IOnGestureListener
{
private GestureDetector _gestureDetector;
public bool OnDown(MotionEvent e)
{
return false;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
bool result = false;
int SWIPE_THRESHOLD = 80;
int SWIPE_VELOCITY_THRESHOLD = 80;
try
{
float diffY = e2.GetY() - e1.GetY();
float diffX = e2.GetX() - e1.GetX();
if (Math.Abs(diffX) > Math.Abs(diffY))
{
if (Math.Abs(diffX) > SWIPE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
{
if (diffX > 0)
{
//code for swipe right here (this would is different for each ImageView)
previousImage();
}
else
{
//code for swipe Left here (this would is different for each ImageView)
nextImage();
}
}
}
}
catch (Exception exception)
{
//exception.printStackTrace();
}
return result;
}
public void OnLongPress(MotionEvent e) {}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
public void OnShowPress(MotionEvent e) {}
public bool OnSingleTapUp(MotionEvent e)
{
return false;
}
public override bool OnTouchEvent(MotionEvent e)
{
_gestureDetector.OnTouchEvent(e);
return false;
}
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
_gestureDetector = new GestureDetector(this);
}
但是,当我尝试对
Fragment
执行相同操作时,我发现OnTouchEvent
中没有Override
。同样在Activity
中,gestureListener
正在监听整个屏幕,现在我希望它在ImageView
上。我怎样才能做到这一点?通过Google搜索,我发现我需要将此答案Detect swipe gesture in fragment转换为C#,但是我无法做到这一点。
最佳答案
尝试这个:
public class YourFragment : Fragment, View.IOnTouchListener, GestureDetector.IOnGestureListener
{
private GestureDetector _gestureDetector;
private ImageView imageView;
public YourFragment()
{
this.RetainInstance = true;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.fragment_profile, null);
imageView = view.FindViewById<ImageView>(Resource.Id.profile_image);
_gestureDetector = new GestureDetector(this);
imageView.SetOnTouchListener(this);
return view;
}
public bool OnTouch(View v, MotionEvent e)
{
return _gestureDetector.OnTouchEvent(e);
}
public bool OnDown(MotionEvent e)
{
//rzee: Changed to true
return true;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
throw new System.NotImplementedException();
}
public void OnLongPress(MotionEvent e)
{
}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
//rzee: Changed to true
return true;
}
public void OnShowPress(MotionEvent e)
{
}
public bool OnSingleTapUp(MotionEvent e)
{
return true;
}
}
我已经测试了自己的身体,运作良好。
干杯!!