zoukankan      html  css  js  c++  java
  • Android SwipeRefreshLayout

    首先介绍一下 SwipeRefreshLayout ,由于下拉刷新使用的人比较多,于是谷歌自己就做了一个下拉刷新的控件.

    android.support.v4.widget.SwipeRefreshLayout

    具体是使用方法:

    //XML
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.swiperefresh.MainActivity" >
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/srl"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <ListView
                android:id="@+id/lv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </android.support.v4.widget.SwipeRefreshLayout>
    
    </LinearLayout>
    //Activity
    package com.example.swiperefresh;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
    
        private ListView lv;
        private SwipeRefreshLayout srl;
        private ArrayAdapter<String> adapter;
        private ArrayList<String> date;
        private int count = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            date = new ArrayList<String>();
            initUI();
    
        }
    
        private void initUI() {
            lv = (ListView) findViewById(R.id.lv);
            srl = (SwipeRefreshLayout) findViewById(R.id.srl);
            srl.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
            srl.setOnRefreshListener(new OnRefreshListener() {
    
                @SuppressWarnings("unchecked")
                @Override
                public void onRefresh() {
                    new listAsyncTask().execute();
                }
            });
            adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
                    date);
            lv.setAdapter(adapter);
        }
    
        public class listAsyncTask extends AsyncTask {
    
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                srl.setRefreshing(true);
            }
    
            @Override
            protected Object doInBackground(Object... params) {
                // TODO Auto-generated method stub
                return count++;
            }
    
            @Override
            protected void onPostExecute(Object result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                date.add(0, "" + result);
                adapter.notifyDataSetChanged();
                srl.setRefreshing(false);
            }
        }
    
    }
  • 相关阅读:
    15年双11手淘前端技术分享(转)
    高程第9章 客户端检测
    高程8.4 screen对象 8.5history对象 8.6小结
    高程8.2location对象 8.3navigator对象
    高程第8章 BOM 8.1window对象
    高程 7.3 模仿块级作用域 7.4私有变量 7.5小结
    高程 第7章函数表达式 7.1递归 7.2闭包
    23、GoAccess分析Nginx日志
    11、Nginx反向代理服务
    10、LNMP架构
  • 原文地址:https://www.cnblogs.com/stareblankly/p/5033275.html
Copyright © 2011-2022 走看看