2016-04-15 52 views
0

我想要做类似Facebook的事情,当我们拖动滚动视图直到y为负时,活动指示器将显示并刷新视图。滑动以刷新ScrollView

当我实现下面的代码,它在IOS上工作。但是,在Android设备上运行时,滚动y似乎只能有0或大于0的值,并且不能滚动滚动到负面区域。

async void ContentScrollView_Scrolled(object sender, ScrolledEventArgs e) 
{ 
    if (!hold) 
    { 
     if (ContentScrollView.ScrollY < -70) 
     { 
      hold = true; 
      scrollLoadingBar.IsRunning = true; 
      scrollLoadingBar.IsVisible = true; 

      netStructure structure = await Net.getWebServicesData("chart.asmx/GetCampaignChart?username=" + App.username + "&password=" + App.password + "&campid=" + App.campid + "&noOfChart=1&callback="); 
      if (structure.status) 
      { 
       App.chart1 = structure.returnObject; 
       setcampaignchart(); 
      } 
      else 
      { 
       await DisplayAlert("Loading Chart Error", structure.errorMessage, "Ok"); 
      } 

      scrollLoadingBar.IsVisible = false; 
      scrollLoadingBar.IsRunning = false; 
     } 
    } 
    if (hold) 
    { 
     if (ContentScrollView.ScrollY == 0) 
     { 
      hold = false; 
     } 
    } 
} 

我该如何得到这个在Android上的工作?

回答

1

我猜你想要一个Xamarin.Forms解决方案,即使你没有在你的问题中提到过,因为Xamarin.Forms标签。你不需要手工编码。 James Montemagno为我们完成了所有艰苦的工作。这里是链接到PullToRefreshLayout github页面。您只需要在Xaml的ScrollView位置添加PullToRefreshLayout,并将ScrollView设置为Content。

编辑:解释@cheesebaron建议的用法。您可以安装插件Nuget包Refractored.XamForms.PullToRefresh

然后,我们可以通过向ScrollView添加PullToRefreshLayout作为父项来实现PullToRefresh操作。我引用Github中的示例页面

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage 
xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="RefreshSample.Views.ScrollViewXamlPage" 
xmlns:controls="clr-namespace:Refractored.XamForms.PullToRefresh;assembly=Refractored.XamForms.PullToRefresh" 
Title="Xaml Scroll"> 
<controls:PullToRefreshLayout 
     IsPullToRefreshEnabled="True" 
     RefreshCommand="{Binding RefreshCommand}" 
     IsRefreshing="{Binding IsBusy}" 
     RefreshColor="Blue"> 
     <ScrollView 
     HorizontalOptions="FillAndExpand" 
     VerticalOptions="FillAndExpand"> 
     <StackLayout 
       HorizontalOptions="FillAndExpand" 
       VerticalOptions="FillAndExpand"> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Blue"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Red"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Yellow"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Purple"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Maroon"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Blue"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Red"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Yellow"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Purple"/> 
      <BoxView HeightRequest="100" WidthRequest="100" BackgroundColor="Maroon"/> 
     </StackLayout> 
     </ScrollView> 
    </controls:PullToRefreshLayout> 
</ContentPage> 
+0

请使用此库添加更多描述。作为答案,它的质量很低,并且不符合StackOverflow质量 – Cheesebaron

+0

请问如何在项目中实施他的工作? –

+0

@RyanShine右键单击包文件夹>添加包。 Add Packages窗口将弹出。搜索Refractored.XamForms.PullToRefresh。图书馆将在左边部分出现。选择它然后点击Add Packages。现在库将被添加到您的项目中,然后您可以参考示例 – Sreeraj