<1>、MainActivity代码
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private List<String> list;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
adapter.notifyDataSetChanged();
close();
}
};
private MyAdapter adapter;
private XListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (XListView) findViewById(R.id.lv);
initDate();
lv.setPullLoadEnable(true);
lv.setPullRefreshEnable(true);
lv.setXListViewListener(this);
adapter = new MyAdapter();
lv.setAdapter(adapter);
}
public void close(){
lv.stopRefresh();
lv.stopLoadMore();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH:mm:ss");
String format = simpleDateFormat.format(new Date());
lv.setRefreshTime(format);
}
private void initDate() {
list=new ArrayList<>();
for(int i=0;i<10;i++){
list.add("想要宝马*"+i);
}
}
/**
* 下拉刷新
*/
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
list.add(0,"送你一辆宝马X6");
handler.sendEmptyMessage(0);
}
},2000);
}
/**
* 上拉加载更多
*/
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
list.add("送您多个豪车");
handler.sendEmptyMessage(0);
}
},2000);
}