gpt4 book ai didi

c# - OnGestureListener 在 Xamarin.Android 上的 fragment 中的 ListView 上捕获

转载 作者:行者123 更新时间:2023-11-29 20:24:59 26 4
gpt4 key购买 nike

我在 Activity 上创建了一个手势 Listener,效果很好:

[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 上执行相同操作时,我发现没有要OverrideOnTouchEvent >。同样在 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;
}
}

我已经测试了我这边,工作得很好。

干杯!

关于c# - OnGestureListener 在 Xamarin.Android 上的 fragment 中的 ListView 上捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32671997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com