android 下拉刷新控件:SwipeRefreshLayout

1、布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/main_swiperefreshlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
        <ListView
            android:id="@+id/main_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

2、初始化:

 final List<String> list=new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("当前的item为 " + i);
        }
        adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);

        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light);
        //给swipeRefreshLayout绑定刷新监听
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //设置1.5秒后,来执行以下事件
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        list.add(0, "刷新后新增的item");
                        adapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 1500);
            }
        });

感觉可当做linearlayout来用

-----------------------------------------更新更方便的:
依赖:implementation ‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1’

布局:

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/myRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    </android.support.v4.widget.SwipeRefreshLayout>

android.support.design.widget.AppBarLayout$ScrollingViewBehavior
代码:

private SwipeRefreshLayout mSwipRefreshLayout;
SwipeRefreshLayout.OnRefreshListener refreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            page = 1;
            getSearchType(good);
        }
    };

mSwipRefreshLayout = (SwipeRefreshLayout) findviewById(R.id.search_refresh_layout);
mSwipRefreshLayout.setOnRefreshListener(refreshListener);

在数据请求回来之后:

 if (mSwipRefreshLayout.isRefreshing()) {
       mSwipRefreshLayout.setRefreshing(false);
}

也可以是这样:

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/mine_refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F0F0F0"
            android:orientation="vertical">
        </LinearLayout>

    </android.support.v4.widget.SwipeRefreshLayout>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值