android listView 滑动加载数据 该数据是服务端获取的
时间:2014-04-30 22:27:40
收藏:0
阅读:456
package com.sunway.works.applycash;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sunway.works.R;
import com.sunway.works.applycash.ApplyCashListActivity;
import com.sunway.works.cost.apply.EditExpenseActivity;
import com.sunway.works.cost.apply.ExpenseAuditActivity;
import com.sunway.works.entitys.ApplyCashEntity;
import com.sunway.works.entitys.IousEntity;
import com.sunway.works.network.HttpService;
import com.sunway.works.network.HttpServiceImpl;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.Service;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class EditOneCashActivity extends Activity implements
OnScrollListener {
private ListView listView;
private Context context;
private View top_title;
private TextView tvtitle;
private PopupWindow popupWindow;
private View view;
private View bar1;
private View bar2;
private Spinner sp_depart;
private EditText ed_iounnub;
private EditText ed_purpose;
private EditText ed_date_from;
private EditText ed_date_over;
private Button btn_date_from;
private Button btn_date_over;
private EditText ed_iousum;
private Spinner sp_checkstate;
private EditText ed_borrower;
private Button btn_query;
private Handler handler;
private static final int DATE_DIALOG_ID = 1;
private static final int SHOW_DATAPICK = 0;
private int mYear;
private int mMonth;
private int mDay;
private DeleteCashListAdapter adapter;
private List<ApplyCashEntity> listdata = new ArrayList<ApplyCashEntity>();
private InputMethodManager imm;
private Button btn_left;
private Button btn_right;
private List<ApplyCashEntity> loadDatas; // 加载数据
// private ChooseCheckmanAdapter adapters; //适配器
private LinearLayout loadingLayout;// 底部加载布局
private Map<Integer, Boolean> isCheckedMap = new HashMap<Integer, Boolean>();;
private boolean over;// 判断数据是否已全部加载
private Thread mThread;// 加载线程
private int unb = 1;
// 设置布局显示属性
private android.widget.LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// 设置布局显示目标最大化属性
private android.widget.LinearLayout.LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
private PopupWindow window;
private Intent intent;
private ListView list2;
Handler mhandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:// 判断是否数据已全部加载
if (!over) {
adapter.notifyDataSetChanged();
} else {
listView.removeFooterView(loadingLayout);
Toast.makeText(context, "数据加载完毕", Toast.LENGTH_SHORT)
.show();
}
break;
case 2:
adapter.notifyDataSetChanged();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_data_list);
context = getApplicationContext();
handler = new Handler();
// 初始化底部加载视图
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
progressBar = new ProgressBar(context);
progressBar.setPadding(0, 0, 15, 0);
layout.addView(progressBar, mLayoutParams);
TextView textView = new TextView(context);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(textView, FFlayoutParams);
loadingLayout = new LinearLayout(context);
loadingLayout.addView(layout, mLayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
btn_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
private void initView() {
// TODO Auto-generated method stub
top_title = this.findViewById(R.id.top_title);
tvtitle = (TextView) top_title.findViewById(R.id.tvtitle);
btn_left = (Button) top_title.findViewById(R.id.btn_title_left);
btn_right = (Button) top_title.findViewById(R.id.btn_title_right);
listView = (ListView) findViewById(R.id.data_enterlist);
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (mThread == null || !mThread.isAlive()) {
mThread = new Thread() {
listdata.addAll(loadDatas);
loadDatas.clear();
} else {
unb = unb - 1;
over = true;
}
unb++;
mhandler.sendEmptyMessage(1);//更新UI
};
};
mThread.start();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
}
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sunway.works.R;
import com.sunway.works.applycash.ApplyCashListActivity;
import com.sunway.works.cost.apply.EditExpenseActivity;
import com.sunway.works.cost.apply.ExpenseAuditActivity;
import com.sunway.works.entitys.ApplyCashEntity;
import com.sunway.works.entitys.IousEntity;
import com.sunway.works.network.HttpService;
import com.sunway.works.network.HttpServiceImpl;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.Service;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class EditOneCashActivity extends Activity implements
OnScrollListener {
private ListView listView;
private Context context;
private View top_title;
private TextView tvtitle;
private PopupWindow popupWindow;
private View view;
private View bar1;
private View bar2;
private Spinner sp_depart;
private EditText ed_iounnub;
private EditText ed_purpose;
private EditText ed_date_from;
private EditText ed_date_over;
private Button btn_date_from;
private Button btn_date_over;
private EditText ed_iousum;
private Spinner sp_checkstate;
private EditText ed_borrower;
private Button btn_query;
private Handler handler;
private static final int DATE_DIALOG_ID = 1;
private static final int SHOW_DATAPICK = 0;
private int mYear;
private int mMonth;
private int mDay;
private DeleteCashListAdapter adapter;
private List<ApplyCashEntity> listdata = new ArrayList<ApplyCashEntity>();
private InputMethodManager imm;
private Button btn_left;
private Button btn_right;
private List<ApplyCashEntity> loadDatas; // 加载数据
// private ChooseCheckmanAdapter adapters; //适配器
private LinearLayout loadingLayout;// 底部加载布局
private Map<Integer, Boolean> isCheckedMap = new HashMap<Integer, Boolean>();;
private boolean over;// 判断数据是否已全部加载
private Thread mThread;// 加载线程
private int unb = 1;
// 设置布局显示属性
private android.widget.LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// 设置布局显示目标最大化属性
private android.widget.LinearLayout.LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
private PopupWindow window;
private Intent intent;
private ListView list2;
Handler mhandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:// 判断是否数据已全部加载
if (!over) {
adapter.notifyDataSetChanged();
} else {
listView.removeFooterView(loadingLayout);
Toast.makeText(context, "数据加载完毕", Toast.LENGTH_SHORT)
.show();
}
break;
case 2:
adapter.notifyDataSetChanged();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_data_list);
context = getApplicationContext();
handler = new Handler();
// 初始化底部加载视图
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
progressBar = new ProgressBar(context);
progressBar.setPadding(0, 0, 15, 0);
layout.addView(progressBar, mLayoutParams);
TextView textView = new TextView(context);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(textView, FFlayoutParams);
loadingLayout = new LinearLayout(context);
loadingLayout.addView(layout, mLayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
// 初始化ListView并设定事件
initView();
getListener();
}
private void getListener() {btn_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
private void initView() {
// TODO Auto-generated method stub
top_title = this.findViewById(R.id.top_title);
tvtitle = (TextView) top_title.findViewById(R.id.tvtitle);
btn_left = (Button) top_title.findViewById(R.id.btn_title_left);
btn_right = (Button) top_title.findViewById(R.id.btn_title_right);
listView = (ListView) findViewById(R.id.data_enterlist);
listView.addFooterView(loadingLayout);
// listView.addFooterView()方法一定要在其setadapter()方法之前使用。不然报错
adapter = new DeleteCashListAdapter(EditOneCashActivity.this,
listdata, true);
listView.setAdapter(adapter);
listView.setOnScrollListener(EditOneCashActivity.this);
over = false;
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (mThread == null || !mThread.isAlive()) {
mThread = new Thread() {
public void run() {
//从网上读取要获得的数据 这是我自己的读取server的数据,个人因需要自己写自己的联网读取数据
HttpService service = new HttpServiceImpl();String i = (10 * (unb - 1) + 1) + ""; // i 是下标,决定从哪一条数据开始读,10代表每一次读取数据的条数
loadDatas = service.getApplyCashList(param1, i);
// 如果加载数据不为空,则将加载得到的数据添加到已有的mData数据中.否则数据加载完全
listdata.addAll(loadDatas);
loadDatas.clear();
} else {
unb = unb - 1;
over = true;
}
unb++;
mhandler.sendEmptyMessage(1);//更新UI
};
};
mThread.start();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
}
评论(0)