zoukankan      html  css  js  c++  java
  • Xamarin android SwipeRefreshLayout入门实例

    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

  • 相关阅读:
    设计模式-代理模式
    设计模式-策略模式
    设计模式-单例模式
    优先队列
    n!中质因子个数
    计算组合数
    高精度
    memset用法
    质因子分解
    素数筛法
  • 原文地址:https://www.cnblogs.com/zhangmumu/p/7374801.html
Copyright © 2011-2022 走看看