【问题标题】:Xamarin Forms Slider in ListView in TabbedPage not sliding correctlyTabbedPage 中 ListView 中的 Xamarin 表单滑块无法正确滑动
【发布时间】:2020-07-25 17:15:36
【问题描述】:

我有一个 DataTemplate,它将滑块放在 TabbedPage 内的 ListView 内。在 Android 上,滑块无法正确移动,从左向右滑动会激活 TabbedPage 滑动功能。如果滑块在 ListView 之外,它可以正常工作。如何防止滑块的父控件处理拖动手势?

ViewParent.requestDisallowInterceptTouchEvent(boolean) 方法可能会有所帮助,但我会使用哪个渲染器等?

Video of issue with the slider

【问题讨论】:

    标签: android xamarin xamarin.android xamarin.forms xamarin.forms.listview


    【解决方案1】:

    创建一个SliderRenderer,然后在DispatchTouchEvent(MotionEvent e)中调用RequestDisallowInterceptTouchEvent(boolean),就可以解决这个问题,下面是我的代码:

    MainPage:

            var tabsXaml = new TabbedPage { Title = "Working with ListView" };
            tabsXaml.Children.Add(new BasicListXaml { Title = "Basic", Icon = "icon.png" } );
            tabsXaml.Children.Add(new JustView { Title = "JustView", Icon = "icon.png" });
            tabsXaml.Children.Add(new UnevenRowsXaml { Title = "UnevenX", Icon = "icon.png" });
            tabsXaml.Children.Add(new SliderPage { Title = "SliderPage", Icon = "icon.png" });
            MainPage = tabsXaml;
    

    TabbedPage中的第二页:

    <ContentPage.Content>
        <local:MyListView x:Name="listView" ItemSelected="OnItemSelected">
            <local:MyListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <local:MySlider HorizontalOptions="Fill"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </local:MyListView.ItemTemplate>
        </local:MyListView>
    </ContentPage.Content>
    

    SlidererRenderer:

       public override bool DispatchTouchEvent(MotionEvent e)
        {
            switch (e.Action)
            {
                case MotionEventActions.Down:
                    Parent.Parent.Parent.Parent.Parent.Parent.Parent.RequestDisallowInterceptTouchEvent(true);                    
                    break;
                case MotionEventActions.Move:
                     //This is the core of the problem!!!
                    Parent.Parent.Parent.Parent.Parent.Parent.Parent.RequestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEventActions.Up:
                    break;
                default:
                    break;
            }
            return base.DispatchTouchEvent(e);
        }
    

    您必须调用RequestDisallowInterceptTouchEvent(true) 方法。我的代码中的Parent.Parent.Parent.Parent.Parent.Parent.Parent 表示Xamarin.Forms.Platform.Android.PageRendererSlider 不能在video 中正常工作的原因是page(在我的代码中是 JustView)阻止滑块处理拖动手势。

    【讨论】:

    • 很好的答案!你救了我免于拔掉我所有的头发,谢谢!
    【解决方案2】:

    我的答案基于 York Shen 提供的解决方案。我不喜欢硬编码父嵌套的方式,所以我只是添加了一个函数来帮助找到我们要调用RequestDisallowInterceptTouchEvent 的渲染器。我的 Slider 嵌套在另一个视图中,这意味着我需要一个不同级别的父级。所以给你:

    public override bool DispatchTouchEvent(MotionEvent e)
    {
        switch (e.Action)
        {
            case MotionEventActions.Down:
                (_navigationPage ??= GetNavigationPage(Parent)).RequestDisallowInterceptTouchEvent(true);
                break;
            case MotionEventActions.Move:
                //This is the core of the problem!!!
                _navigationPage.RequestDisallowInterceptTouchEvent(true);
                break;
            case MotionEventActions.Up:
                break;
            default:
                break;
        }
        return base.DispatchTouchEvent(e);
    }
    
    private IViewParent _navigationPage;
    
    private IViewParent GetNavigationPage(IViewParent parent)
    {
        if (parent is NavigationPageRenderer)
            return parent;
        return GetNavigationPage(parent.Parent);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      相关资源
      最近更新 更多