猿问

拉动刷新(SwipeRefreshLayout)android xamarin c#

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


一开始,axml 是:


初始代码

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

我添加了 swipeRefreshLayout 所以:

代码滑动刷新


在课堂上,我添加了以下代码:


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


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 页面时应用程序崩溃。错误是:

一只名叫tom的猫
浏览 191回答 1
1回答

当年话下

只要用户可以通过垂直滑动手势刷新视图的SwipeRefreshLayout内容,就应该使用 。阅读更多所以,SwipeRefreshLayout它的行为就像一个容器,所以SwipeRefreshLayout你应该在里面添加一个ListVieworGridView来让它工作,如下所示:请记住,SwipeRefreshLayout只支持一个ListView或一个GridView孩子。<LinearLayout&nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; android:layout_width="match_parent"&nbsp; android:layout_height="match_parent"&nbsp; android:orientation="vertical">&nbsp; <android.support.v4.widget.SwipeRefreshLayout&nbsp; &nbsp; &nbsp;android:id="@+id/swipeRefreshLayout"&nbsp; &nbsp; &nbsp;android:layout_width="match_parent"&nbsp; &nbsp; &nbsp;android:layout_height="match_parent">&nbsp; &nbsp; &nbsp; &nbsp;<ListView&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:id="@+id/list"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="match_parent"/>&nbsp; </android.support.v4.widget.SwipeRefreshLayout></LinearLayout>然后,设置您的视图元素:SwipeRefreshLayout swipeRefreshLayout;ListView listView;&nbsp; &nbsp;&nbsp;protected override void OnCreate(Bundle savedInstanceState){&nbsp; &nbsp;// ...&nbsp; &nbsp;listView = view.FindViewById<ListView>(Resource.Id.list);&nbsp; &nbsp;swipeRefreshLayout = view.FindViewById<SwipeRefreshLayout>(Resource.Id.swipeRefreshLayout);&nbsp; &nbsp;// Elevation helps users understand the relative importance of each element and focus their attention to the task at hand. (Optional)&nbsp; &nbsp;// swipeRefreshLayout.Elevation = 10;&nbsp;&nbsp; &nbsp;swipeRefreshLayout.Refresh += delegate (object sender, System.EventArgs e)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; // logic...&nbsp; &nbsp;};&nbsp; &nbsp;// ...}此外,您可以通过以下方式手动触发SwipeRefreshLayout:swipeRefreshLayout.Post(() => {&nbsp;&nbsp; &nbsp;swipeRefreshLayout.Refreshing = true;&nbsp;&nbsp; &nbsp;listView.Clickable = false;&nbsp;});// logic...swipeRefreshLayout.Post(() => {&nbsp;&nbsp; &nbsp;swipeRefreshLayout.Refreshing = false;&nbsp;&nbsp; &nbsp;listView.Clickable = true;&nbsp;});我希望这可以帮助你。
随时随地看视频慕课网APP
我要回答