SwipeRefreshLayout实现下拉刷新

新建布局文件list_refresh.xml

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fram_list"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
//将布局文件和java文件绑定在一起
tools:context="com.example.doubandemo.ListRefresh"
>
<ListView
    android:id="@+id/list_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></ListView>
</android.support.v4.widget.SwipeRefreshLayout>

新建ListRefreshFragment类继承Fragment类实现SwipeRefreshLayout.OnRefreshListener接口,重写onRefresh()方法

public void onRefresh() {
    mSwipeRefreshLayout.setRefreshing(true);
    new Handler().postDelayed(new Runnable() {
        public void run() {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }, 3000);
}

然后在onCreateView()中加载布局

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //加载布局文件
    View view = inflater.inflate(R.layout.list_refresh,container,false);
    //初始化数据
    initData();
    mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.fram_list);
    //为下拉刷新的滚动条设置颜色,可以设置4种
    mSwipeRefreshLayout.setColorSchemeResources(
            android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light
    );
    //因为实现了OnRefreshListener接口,所以这里可以传入this
    mSwipeRefreshLayout.setOnRefreshListener(this);
    ListView listView = (ListView) view.findViewById(R.id.list_item);
    //填充ListView
    listView.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listData));
    return view;
}

在MainActivity中加载Fragment

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值