android - Xamarin.Forms。像 Telegram 一样滑动到上一页

标签 android xamarin.ios swipe

如何创建向后滑动?

在 iOS 中:

[assembly: ExportRenderer(typeof(HIPSTO.Controls.CustomContentPage), typeof(HIPSTO.iOS.Platform.Renderers.IOSPageRenderer))]     
namespace HIPSTO.iOS.Platform.Renderers 
{
    public class IOSPageRenderer : PageRenderer        
    {         
      public override void ViewWillAppear(bool animated)            
      {              
        base.ViewWillAppear(animated);

        ViewController.NavigationController.InteractivePopGestureRecognizer.Enabled = true;
        ViewController.NavigationController.InteractivePopGestureRecognizer.Delegate = new UIKit.UIGestureRecognizerDelegate();
       }
    }
}

但它只适用于边缘。从任何地方都需要。 对安卓没有想法

如下图:

enter image description here

最佳答案

首先,你可以通过android中的NavigationPageRenderer来实现。

这是演示的动图

enter image description here

TransitionNavigationPageRender.cs

[assembly: ExportRenderer(typeof(SwipePageDemo.Controls.TransitionNavigationPage), typeof(TransitionNavigationPageRenderer))]
namespace SwipePageDemo.Droid.Renderers
{
public class TransitionNavigationPageRenderer : NavigationPageRenderer
{


    public TransitionNavigationPageRenderer(Context context) : base(context)
    {
    }



    protected override void SetupPageTransition(Android.Support.V4.App.FragmentTransaction transaction, bool isPush)
    {

                if (isPush)
                {

                }
                else
                {
                    transaction.SetCustomAnimations(Resource.Animation.enter_left, Resource.Animation.exit_right,
                                                    Resource.Animation.enter_right, Resource.Animation.exit_left);
                }

    }
}
}

enter_left.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:propertyName="enter_from_left"
  android:shareInterpolator="false">
<translate
  android:fromXDelta="-100%" android:toXDelta="0%"
  android:fromYDelta="0%" android:toYDelta="0%"
  android:duration="300"/>
</set>

enter_right.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
  <translate
     android:fromXDelta="100%" android:toXDelta="0%"
     android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="300" />
</set>

exit_left.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
 <translate
  android:fromXDelta="0%" android:toXDelta="-100%"
  android:fromYDelta="0%" android:toYDelta="0%"
  android:duration="300"/>
</set>

exit_right.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
  android:fromXDelta="0%" android:toXDelta="100%"
  android:fromYDelta="0%" android:toYDelta="0%"
  android:duration="300" />
</set>

如果你想了解更多关于 Page Transition 的细节,你可以引用这个链接。 https://github.com/jsuarezruiz/xamarin-forms-page-transitions

但它只适用于边缘。从任何地方都需要

你可以通过 SwipeGestureRecognizer 类的 threshold 属性来实现它

The SwipeGestureRecognizer class also includes a Threshold property ,可以选择将其设置为一个 uint 值,该值表示要识别滑动必须达到的最小滑动距离,以设备无关的单位表示。此属性的默认值为 100,这意味着任何小于 100 个设备独立单位的滑动都将被忽略。如果你想增加可以滑动的区域,你可以像下面的代码一样减少Threshold的这个数据。

        var leftSwipeGesture = new SwipeGestureRecognizer { Direction = SwipeDirection.Right };
        leftSwipeGesture.Threshold = 50;
        leftSwipeGesture.Swiped += (sender, e) => Navigation.PopAsync();
       stackLayout.GestureRecognizers.Add(leftSwipeGesture);

我将 leftSwipeGesture.Threshold 的值更改为 50,这是滑动的 GIF。 enter image description here

关于android - Xamarin.Forms。像 Telegram 一样滑动到上一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54184584/

相关文章:

css - 在 CSS 中定义 DIV 宽度以允许在 iPad2 中滚动/滑动

android - TabHost,滑动 Intent 失败

android - 使用 AsyncTask 进行 sleep

interface-builder - 将 socket 添加到 iPhone View

c# - UIImageView.Image 使用 MonoTouch 发送邮件附件

c# - 使用 Xamarin/Vision 框架进行 iOS 条码检测

android - 导航标签 - 滑动标签(固定标签)

Android Wear - 如何为 WearableListView 设置分隔空间

android - ImageView adjustViewBounds 好像不行??(不同项目不同结果)

java - 无法执行 android :onClick (TextView. setText 的方法)