android 下拉刷新控件:SwipeRefreshLayout

1、布局:



    
        
    


2、初始化:

 final List 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.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>

你可能感兴趣的:(android 下拉刷新控件:SwipeRefreshLayout)