微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwipeView 仅在执行滑动后对某些行做出响应! 直到页面 manullay 导航

如何解决SwipeView 仅在执行滑动后对某些行做出响应! 直到页面 manullay 导航

我有一个标题模板组成的复杂列表视图,用于对集合中的集合进行分组,我使用了下面的教程,该教程使用了在列表视图中按酒店对房间进行分组的示例:

https://www.c-sharpcorner.com/article/xamarin-forms-expandable-listview-with-a-sub-listview-mvvm-pattern/

我已将 swipeview 添加到项目中,但在一次滑动以从子集合中删除项目后,滑动手势仅对随机行做出响应。

这似乎是一个错误,可能是由于我使用了分层模型/视图模型结构

对此有什么建议或提示吗?我同时使用了 SwipeItems 和 SwipeItemView,但都没有区别。此外,有时网格中的第一个项目不会滑动,但最后一个项目会......

在从集合中删除项目后,我还使用了 InitialiseComponent,但我不喜欢页面滚动到顶部的方式,而且 scrolltoasync 看起来也很棒。

我的 ListView 的 ItemSource 是一个 ObservableCollection,其中包含子 Observable 集合 - LogItems、FictionList、HistoryList 等。当我获取检索到的项目滑动时,对子集合执行 foreach 以将其从集合和 UI 中删除相应更新。但是,并非所有项目都可以滑动......

我一直在努力解决这个错误,所以任何帮助将不胜感激。

    <Grid x:Name="Grid2" Margin="0,-23,0" 
             MinimumHeightRequest="500" 
              BackgroundColor="Transparent"
              VerticalOptions="Fill" >
            <Grid.RowDeFinitions>
                <RowDeFinition Height="*"/>
                <RowDeFinition Height="30"/>
            </Grid.RowDeFinitions>
            <Grid.ColumnDeFinitions>
                <ColumnDeFinition Width="*"/>
            </Grid.ColumnDeFinitions>

            <StackLayout x:Name="Stack" Padding="0,15,1,20" 
             BackgroundColor="Transparent"
                         VerticalOptions="Start" HeightRequest="1200" 
                         >
                    <ListView
                        x:Name="categoriesList"
                        Grid.Row="1"
                        HasUnevenRows="True"
                        HeightRequest="10"
                        BackgroundColor="White"
                        IsGroupingEnabled="True"
                        IsPullToRefreshEnabled="false"
                        ItemsSource="{Binding LogItems}"
                        IsRefreshing="{Binding IsBusy,Mode=OneWay}"
                        RefreshCommand="{Binding ExecuteRefreshItemsCommand}"
                            SeparatorVisibility="None"                      
                            >
    <ListView.ItemTemplate>
              <DataTemplate >
           

        <ViewCell Height="70" >
                    <StackLayout Orientation="Vertical" 

    VerticalOptions="Start" BackgroundColor="White">                                        
                             <swipeview>
                                  <swipeview.RightItems>
                                       <SwipeItems Mode="Reveal" SwipeBehaviorOnInvoked="Close">
                                            <SwipeItem
                                                 Text = "Delete" IconImageSource="deleteIcon3.png" BackgroundColor="Red" Invoked="swipeview_SwipeInitiated"/>                      
                                       </SwipeItems>
       

                            </swipeview.RightItems>
                               <swipeview.Content>  
                                    <Grid> 

        <Label
        Grid.Row="1"                                                          
        Grid.Column="0"
        VerticalOptions="Center"
        FontAttributes="None"                                       
        Text="{Binding .Log.Name }"
        TextColor="Black"
        Margin="0,0"
        Padding="10,10,0"
        FontFamily="Hiragino Sans"
        FontSize="14"
        HeightRequest="53"
        BackgroundColor="White"
        />

...

</Grid>


Code behind

        public void swipeview_SwipeInitiated(object sender,EventArgs e)
        {
            var item = sender as SwipeItemView;
            myLogsModel model = item.BindingContext as myLogsModel;

                if (model.BookCategory == "Fiction")
                {
                    foreach (var q in viewmodel.FictionList)
                    {
                        if (q.Name == model.Name)
                        {
                            viewmodel.BookList.Remove(q);                                                                                                                                                                                                              
                            break;
                        }
                    }
                }

 //model - myLogsModel
        if (model.Log.LogCategory == "Fiction")
        {
            foreach (var q in viewmodel.FictionList)
            {
                if (q.MealName == model.Log.Name)
                {
                    viewmodel.FictionList.Remove(q);
                    NotifyPropertyChanged();
                    viewmodel.DeleteLog(q);   
                    break;
                }
            }
        }

然后,我使用 try-catch 清除列表并重新添加有效的新子集合数据:

 try
        {
            if (viewmodel.IsBusy)
                return;
            viewmodel.IsBusy = true;
            viewmodel.LogItems.Clear();

            ObservableCollection<BookTypes> categoryitems = new ObservableCollection<BookTypes>() {
             new BookTypes("Fiction",viewmodel.FictionList),new BookTypes("History",viewmodel.HistoryList),new BookTypes("Politics",viewmodel.Politicslist),};

            if (categoryitems != null && categoryitems.Count > 0)
            {   
                viewmodel.LogItems.Add(new LogTypesModel(types));
                viewmodel.OnPropertyChanged("LogItems");
            }
            else { viewmodel.IsEmpty = true; }
        }
        catch (Exception ex)
        {
            IsBusy = false;
            Debug.WriteLine(ex);
        }
        finally
        {
            viewmodel.IsBusy = false;
            OnPropertyChanged();
        }

解决方法

您需要使用 ObservableCollection 而不是 IListObservableCollection 还实现了 INotifyCollectionChanged 接口,而 List 没有.当我们添加或删除项目时,它会更新 UI。

在您的情况下,当您从 categoryitems 中删除项目时。列表视图不会刷新。所以不会再调用 swipe 事件。

有关更多详细信息,您可以查看此thread

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?