Google SwipeRefreshLayout(Goolge官方下拉刷新控件)尝鲜

时间:2024-01-18 09:55:02

前天Google官方终于出了Android刷新控件——SwipeRefreshLayout。

使用前先需要将android.support.v4.jar升级到19.1。升级后,可能会出现SDK版本与Android ADT Bundle

版本不一致从而导致ADT Bundle无法使用的情况。

解决方法可以参照上文:升级SDK Manager后引起的Android Developer ToolKit版本不一致问题

SwipeRefreshLayout官方文档网址:http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

其中有针对本下拉刷新控件详细的说明。重点的是要实现内部接口SwipeRefreshLayout.OnRefreshListener,并针对SwipeRefreshLayout控件

这是刷新的监听器(通过setOnRefreshListener方法)。同时也提供了对事件的处理以及View的绘制处理等。

简单demo如下:

Java代码:

 package com.corn.swiperefreshlayoutdemo;

 import com.storm.swiperefreshlayoutdemo.R;

 import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log; public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout swipeLayout; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.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);
} public void onRefresh() {
Log.w("test", "in onRefresh");
}
}

Xml布局文件:

 <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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="@string/hello_world" />
</ScrollView> </android.support.v4.widget.SwipeRefreshLayout>