【问题标题】:Pull to refresh (SwipeRefreshLayout) android xamarin c#拉动刷新(SwipeRefreshLayout)android xamarin c#
【发布时间】:2018-09-13 08:25:38
【问题描述】:

我需要在主页屏幕中添加拉动刷新。

开头的axml是:

initial code

  [1]:https://pastebin.com/iZNFiqgt

我添加了 swipeRefreshLayout 所以:

Code SwipeRefresh

在课堂上我添加了这段代码:

............

SwipeRefreshLayout refreshLayout

protected override void OnCreate (Bundle savedInstanceState)
{
.........

refreshLayout = FindViewById<SwipeRefreshLayout> . 
(Resource.Id.swipeRefreshLayout1);

refreshLayout.Refresh += RefreshLayout_Refresh

}

void RefreshLayout_Refresh(object sender, EventArgs e)
{
 //insert action pull to refresh 
Console.WriteLine("start pull to refresh"); //after delete it and 
insert the action 

}

但是当我尝试 pullRefresh 页面时应用程序崩溃。错误是:

感谢大家的支持!!

【问题讨论】:

  • 将列表视图放入SwipeRefreshLayout 并检查。
  • 我添加了 ListView:pastebin.com/dYDzUai6 但我遇到了同样的错误。你怎么看待这件事??非常感谢。

标签: c# android visual-studio xamarin xamarin.android


【解决方案1】:

只要用户可以通过垂直滑动手势刷新视图的内容,就应该使用SwipeRefreshLayout。 Read more

所以,SwipeRefreshLayout 的行为就像一个容器,所以在 SwipeRefreshLayout 内你应该添加一个 ListView 或 GridView 以使其工作,如下所示:

请记住,SwipeRefreshLayout 仅支持单个 ListView 或 GridView 子级。

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <android.support.v4.widget.SwipeRefreshLayout
     android:id="@+id/swipeRefreshLayout"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

       <ListView 
         android:id="@+id/list"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>

  </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

然后,设置您的视图元素:

SwipeRefreshLayout swipeRefreshLayout;
ListView listView;    

protected override void OnCreate(Bundle savedInstanceState)
{
   // ...

   listView = view.FindViewById<ListView>(Resource.Id.list);
   swipeRefreshLayout = view.FindViewById<SwipeRefreshLayout>(Resource.Id.swipeRefreshLayout);

   // Elevation helps users understand the relative importance of each element and focus their attention to the task at hand. (Optional)
   // swipeRefreshLayout.Elevation = 10; 

   swipeRefreshLayout.Refresh += delegate (object sender, System.EventArgs e)
   {
      // logic...
   };

   // ...
}

另外,您可以通过以下方式手动触发SwipeRefreshLayout:

swipeRefreshLayout.Post(() => { 
   swipeRefreshLayout.Refreshing = true; 
   listView.Clickable = false; 
});

// logic...

swipeRefreshLayout.Post(() => { 
   swipeRefreshLayout.Refreshing = false; 
   listView.Clickable = true; 
});

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2015-08-13
    • 2018-09-24
    • 2015-01-04
    • 2017-11-11
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多