android listView 滑动载入数据 该数据是服务端获取的
Posted mfmdaoyou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android listView 滑动载入数据 该数据是服务端获取的相关的知识,希望对你有一定的参考价值。
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
}
}
以上是关于android listView 滑动载入数据 该数据是服务端获取的的主要内容,如果未能解决你的问题,请参考以下文章
Android异步载入学习笔记之四:利用缓存优化网络载入图片及ListView载入优化
android中listview如何支持上下滑动,左右滑动且左右滑动时可以指定固定列数
Android中使用ListView实现分页刷新(线程休眠模拟)(滑动加载列表)