SwipeRefreshLayout 是在V4包里面,首先要先导入V4包,最新的V4包里面才有这控件

首先是布局

 

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:text="@string/hello_world"  >
            
        </TextView>
        </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

SwipeRefreshLayout 中嵌入一个可滑动的控件,可以是scrollview  也可以是listview  gridview

package com.example.swiperrefreshlayout;

import android.os.Bundle;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.text.SpanWatcher;
import android.view.Menu;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

    SwipeRefreshLayout swiper;
    ObjectAnimator oa;
    TextView text;
    private Interpolator accelerator = new AccelerateInterpolator();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swiper=(SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swiper.setOnRefreshListener(new listener());
        text=(TextView) findViewById(R.id.text);
        
        //显示或隐藏刷新进度条
        swiper.setRefreshing(false);
        // 设置进度条的颜色主题,最多能设置四种
        swiper.setColorScheme(android.R.color.holo_blue_bright, 
                android.R.color.holo_green_light, 
                android.R.color.holo_orange_light, 
                android.R.color.holo_red_light);
        
        oa=ObjectAnimator.ofFloat(swiper, "rotationY", 0f,-180f);
        oa.setDuration(600);
        oa.setRepeatCount(1);
        oa.setRepeatMode(ObjectAnimator.REVERSE);
        oa.setInterpolator(accelerator);
    //    Animation an=new Animation();
    }
    class listener implements  OnRefreshListener{

        @Override
        public void onRefresh() {
            // TODO Auto-generated method stub
            oa.start();
            
            if(swiper.isRefreshing()){
                text.setText("正在刷新");
            }else{
                text.setText("停止刷新");
            };
            //swiper.
        }
        
        
        
    }
}

 

 

 

 

posted on 2014-04-25 09:42  青年程序猿  阅读(990)  评论(0编辑  收藏  举报