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>