package com.example.listviewshangxia;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import com.example.listviewshangxia.XListView.IXListViewListener;
public class MainActivity extends Activity implements IXListViewListener {
private XListView mListView;
private ArrayAdapter mAdapter1;
private Handler mHandler;
private ArrayList<String> dlist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** 下拉刷新,上拉加载 */
dlist = new ArrayList<String>();
mListView = (XListView) findViewById(R.id.techan_xListView);// 这个listview是在这个layout里面
mListView.setPullLoadEnable(true);// 设置让它上拉,FALSE为不让上拉,便不加载更多数据
dlist = getData("0");
mAdapter1 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dlist);
mListView.setAdapter(mAdapter1);
mListView.setXListViewListener(this);
mHandler = new Handler();
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "您点击了"+data[position-1], Toast.LENGTH_LONG).show();
}
});
}
/** 初始化本地数据 */
String data[] = new String[] { "三块石----", "关山湖----", "小鹿沟-----","天女山----", "后安腰----" };
String data1[] = new String[] { "抚顺县救兵乡王木村", "抚顺县救兵乡王木村", "抚顺县救兵乡王木村",
"抚顺县救兵乡王木村", "抚顺县救兵乡王木村" };
private ArrayList<String> getData(String flag) {
for (int i = 0; i < data.length; i++) {
// HashMap<String, Object> map = new HashMap<String, Object>();
// map.put("name", data[i]+flag);
// map.put("content", data1[i]);
// map.put("img", R.drawable.ic_launcher);
if("刷新".equals(flag)){
dlist.add(0,data[i]+flag);//把新的数据,插入到第一行
}else{
dlist.add(data[i]+flag);
}
}
return dlist;
}
/** 停止刷新, */
private void onLoad() {
mListView.stopRefresh();
mListView.stopLoadMore();
mListView.setRefreshTime("刚刚");
}
// 刷新
@Override
public void onRefresh() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
//加载当前的第一页
getData("刷新");
mListView.setAdapter(mAdapter1);
onLoad();
}
}, 2000);
}
// 加载更多
@Override
public void onLoadMore() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
//加载下一页数据
getData("加载");
mAdapter1.notifyDataSetChanged();
onLoad();
}
}, 2000);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
this.finish();
}
return false;
}
}