您现在的位置是:首页 > 文章详情

Xamarin android SwipeRefreshLayout入门实例

日期:2016-10-11点击:448

android SwipeRefreshLayout 是实现的效果就是上滑下拉刷新ListView 获取其他控件数据.基本上每个App都有这种效果。Google提供了一个官方的刷新控件SwipeRefreshLayout,当然你得引入V4兼容包哦还不错项目中也用到了。所以就演示一下这个控件怎么使用吧.

还是先看一下Android SwipeRefreshLayout的API吧

谷歌翻译是这么说的:

的SwipeRefreshLayout应该用于每当用户可以通过一个垂直扫掠姿态刷新的图的内容。每当完成刷新姿态刷卡被告知实例这种观点应该添加一个OnRefreshListener的活动。该SwipeRefreshLayout将通知每一个手势再次完成一次每个听者和; 听者负责正确确定何时开始实际内容的刷新。如果侦听确定不应该有一个刷新,它必须调用setRefreshing(假)来取消刷新的任何可视指示。如果活动希望只显示进度动画,它应该调用setRefreshing(真)。要禁用的姿态和进步的动画,在视图上调用的setEnabled(假)。

这种布局应该由将被刷新为手势的结果,只能支持一个直接子视图的父。此视图也将作出手势的目标和将被迫以匹配的宽度,并在此布局提供的高度。该SwipeRefreshLayout不提供无障碍的事件; 相反,必须提供一个菜单项,以允许内容的刷新无论使用该手势。


看一下 效果图:


具体代码如下,先看一下布局页:Main.axml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
MainActivity.cs:

using Android.App; using Android.Runtime; using Android.Widget; using Android.OS; using Android.Support.V4.Widget; namespace SwipeRefreshLayoutDemo { [Activity(Label = "SwipeRefreshLayoutDemo", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { int count = 1; private SwipeRefreshLayout swipeRefreshLayout; private ListView listView; private ArrayAdapter<string> adapter; private JavaList<string> data= new JavaList<string>{"恩比德","拉塞尔","安东尼-戴维斯","西蒙斯"}; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); listView = (ListView)FindViewById(Resource.Id.listView); swipeRefreshLayout = FindViewById<SwipeRefreshLayout>(Resource.Id.swipeRefreshLayout); swipeRefreshLayout.Refresh += (s, e) => { data.Add("张林-布莱恩特"); adapter.NotifyDataSetChanged(); swipeRefreshLayout.SetColorScheme(Android.Resource.Color.HoloRedLight); swipeRefreshLayout.Refreshing=false; }; adapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1,data); listView.Adapter = adapter; } } } 

示例非常简单,体验一下效果而已。也许有人很郁闷了,这个JavaList是什么玩意?如果用List的话,下拉刷新就没有效果。目前就到这儿,过两天去问问牛哥,看是怎么回事。

昨天晚上写的,今天下午来改一改。终于找到了原因,这个Android自带的适配器选项样式对于List可能是个bug,所以呢自己写一个Adapter就可以用List了

示例代码下载: http://download.csdn.net/detail/kebi007/9652257

作者:张林

原文地址:http://blog.csdn.net/kebi007/article/details/52801731

转载随意注明出处



原文链接:https://yq.aliyun.com/articles/611629
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章