首页 > 移动平台 > 详细

android listView 滑动载入数据 该数据是服务端获取的

时间:2017-07-28 21:34:13      阅读:242      评论:0      收藏:0      [点我收藏+]

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);

// 初始化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;
}

@Override
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数据中.否则数据载入全然

if (loadDatas != null && loadDatas.size() > 0) {
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 滑动载入数据 该数据是服务端获取的

原文:http://www.cnblogs.com/mfmdaoyou/p/7252591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!