标签:
首先介绍一下 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); } } }
标签:
原文地址:http://www.cnblogs.com/stareblankly/p/5033275.html