1、布局文件:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/id_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</android.support.v4.widget.SwipeRefreshLayout>
2、在代码中设置SwipeRefreshLayout的一些属性:
//设置刷新圈圈的颜色
swipeLayout.setColorSchemeResources(R.color.holo_blue_bright,
R.color.holo_green_light,
R.color.holo_orange_light,
R.color.holo_red_light);
//设置刷新圈圈的大小,可选SwipeRefreshLayout.LARGE或SwipeRefreshLayout.DEFAULT
swipeLayout.setSize(SwipeRefreshLayout.DEFAULT);
//刷新回调,可在里边放置网络操作,请求成功后调用swipeLayout.setRefreshing(false)方法即可让刷新圈圈消失
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
}).start();
}
});
3、加载更多,其实就是监听Listview的滚动事件,监听到是最后一个条目时,就去加载显示更多的数据。
AbsListView.OnScrollListener OnScrollListenerOne = new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//当滚到最后一行且停止滚动时,执行加载
if (isLastItem && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE ) {
//加载更多
isLastItem = false; //是否是最后一项
Toast.makeText(MainActivity.this,"加载更多",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//判断是否滚到最后一行
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
isLastItem = true;
}
}
};
下边粘上全部代码:
package com.glodon.swiperefreshlayoutdemo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeLayout;
private ListView listview;
private SimpleAdapter simpleAdapter;
private boolean isLastItem = false;//是否是最后一项
private List<Map<String, Object>> mapList = new ArrayList<>();
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
swipeLayout.setRefreshing(false);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
initRefreshView();
}
private void initView() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
swipeLayout = (SwipeRefreshLayout)findViewById(R.id.swipeLayout);
listview = (ListView)findViewById(R.id.id_lv);
simpleAdapter = new SimpleAdapter(this, mapList, android.R.layout.simple_list_item_1, new String[]{"item"}, new int[]{android.R.id.text1});
listview.setAdapter(simpleAdapter);
}
//假数据
private void initData() {
for (int i = 0; i < 25; i++) {
Map<String, Object> map = new HashMap<>();
map.put("item", "item" + i);
mapList.add(map);
}
}
private void initRefreshView() {
//设置刷新圈圈的颜色
swipeLayout.setColorSchemeResources(R.color.holo_blue_bright,
R.color.holo_green_light,
R.color.holo_orange_light,
R.color.holo_red_light);
//设置刷新圈圈的大小,可选SwipeRefreshLayout.LARGE或SwipeRefreshLayout.DEFAULT
swipeLayout.setSize(SwipeRefreshLayout.DEFAULT);
//刷新回调,可在里边放置网络操作,请求成功后调用swipeLayout.setRefreshing(false)方法即可让刷新圈圈消失
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
}).start();
}
});
listview.setOnScrollListener(OnScrollListenerOne);
}
/**
* 上拉加载更多
*/
AbsListView.OnScrollListener OnScrollListenerOne = new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//当滚到最后一行且停止滚动时,执行加载
if (isLastItem && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE ) {
//加载更多
isLastItem = false; //是否是最后一项
Toast.makeText(MainActivity.this,"加载更多",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//判断是否滚到最后一行
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
isLastItem = true;
}
}
};
}