Android自带的下拉刷新组件SwipeRefreshLayout
发布时间:2020-05-30 15:22:17 所属栏目:Java 来源:互联网
导读:Android自带的下拉刷新组件SwipeRefreshLayout
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 也许之前下拉刷新你可能会用到一些第三方开源库,如PullToRefresh,ActionBar-PullToRefresh等,但现在已经有官方的组件了 ---SwipeRefreshLayout,SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,使用起来很方便,可以很方便的实现Google Now的刷新效果。使用官方自带的控件能够保证通用性以及风格。SwipeRefreshLayout是继承ViewGroup,来实现下拉刷新,它可以 包含其他组件,使用过后感觉很简单。 <RelativeLayout 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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.myapplication2.app.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity implements OnRefreshListener{
private SwipeRefreshLayout swipeRefreshLayout;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
swipeRefreshLayout.setRefreshing(false);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swip);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.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);
}
@Override
public void onRefresh() {
handler.sendEmptyMessageDelayed(1,5000);
}
}
在上面的onRefresh()函数中实现获取数据功能以及更新数据,当更新完数据后,调用swipeRefreshLayout.setRefreshing(false);来关闭刷新。 以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
