<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.mydemo.PullActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/mpull"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<ListView
android:id="@+id/mlist"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 PullActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private List list;
private SimpleAdapter simpleAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull);
initView();
setList();
} private void initView(){
swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.mpull);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//
new Handler().postDelayed(new Thread(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false); //刷新之后发送message
Message msg=new Message();
msg.what=1;
mhandle.sendMessage(msg); }
}),3000);
}
});
listView= (ListView) findViewById(R.id.mlist);
}
private Handler mhandle=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1){
Toast.makeText(getApplicationContext(),"刷新成功!",Toast.LENGTH_SHORT).show();
//下拉之后需要加载的数据
List mlist=new ArrayList();
for (int i=0;i<20;i++){
Map map=new HashMap();
map.put("img",R.drawable.img);
map.put("text","下拉数据"+i);
mlist.add(map);
}
list.addAll(mlist);
simpleAdapter.notifyDataSetChanged();
}
}
};
private void setList(){
list=new ArrayList();
for (int i=0;i<20;i++){
Map map=new HashMap();
map.put("img",R.drawable.img);
map.put("text","新数据"+i);
list.add(map);
}
simpleAdapter=new SimpleAdapter(this,list,R.layout.list_item,new String[]{"img","text"},new int[]{R.id.my_img,R.id.my_item});
listView.setAdapter(simpleAdapter);
}
}