SwiperrefreshLayout实现下拉刷新(demo)

来源:互联网 发布:源码天空 编辑:程序博客网 时间:2024/06/06 00:19
SwiperrefreshLayout实现下拉刷新

简介

SwipeRefreshLayout组件只接受一个子组件:即需要刷新的那个组件。它使用一个侦听机制来通知拥有该组件的监听器有刷新事件发生,换句话说我们的Activity必须实现通知的接口。该Activity负责处理事件刷新和刷新相应的视图。一旦监听者接收到该事件,就决定了刷新过程中应处理的地方。如果要展示一个“刷新动画”,它必须调用setRefrshing(true),否则取消动画就调用setRefreshing(false)

效果图:


















下面通过demo来了解一下:

首先是布局:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_container"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <ListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="wrap_content" >        </ListView>    </android.support.v4.widget.SwipeRefreshLayout></LinearLayout></span>

然后就是给ListView添加数据和适配器一系列的操作,我相信大家英嘎都会,下面的是适配器代码:

<span style="font-size:18px;">package com.example.testswiperefreshlayout;import java.util.ArrayList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class ListViewAdapter extends BaseAdapter {private Context context;private ArrayList<String> arrayList;public ListViewAdapter(Context context, ArrayList<String> arrayList) {super();this.context = context;this.arrayList = arrayList;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn arrayList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn arrayList.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubViewHolder holder = null;if (convertView == null) {holder = new ViewHolder();convertView = LayoutInflater.from(context).inflate(R.layout.item,null);holder.textView = (TextView) convertView.findViewById(R.id.textview);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.textView.setText(arrayList.get(position));return convertView;}private final static class ViewHolder {TextView textView;}}</span>
MainActivity代码:
<span style="font-size:18px;">package com.example.testswiperefreshlayout;import java.util.ArrayList;import android.R.array;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;import android.util.Log;import android.widget.ListView;public class MainActivity extends Activity {private SwipeRefreshLayout swipeRefreshLayout;private ListView listView;private ListViewAdapter adapter;private ArrayList<String> arrayList;private int f = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);Log.d("swipeRefreshLayout----->", swipeRefreshLayout.toString());//給刷新頭設置顔色,最多可以設置4個swipeRefreshLayout.setColorScheme(android.R.color.white,android.R.color.holo_green_light,android.R.color.holo_orange_light,android.R.color.holo_red_light);//對刷新的操作swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {@Overridepublic void onRefresh() {// TODO Auto-generated method stubnew Handler().postDelayed(new Runnable() {public void run() {//不讓刷新頭顯示出來swipeRefreshLayout.setRefreshing(false);//刷新時添加的數據,其中0代表在頭部添加,如果不寫上0,就會添加在尾部arrayList.add(0,"---->" + ++f);adapter.notifyDataSetChanged();}}, 3000);}});listView = (ListView) findViewById(R.id.listview);initData();adapter = new ListViewAdapter(this, arrayList);listView.setAdapter(adapter);}//給arraylist添加的數據private void initData() {arrayList = new ArrayList<String>();for (int i = 0; i < 20; i++) {arrayList.add("---->" + i);}}}</span>


源码下载



1 0