ListView 点击加载更多
主要代码
private void findView() {//初始化控件 // TODO Auto-generated method stub lv_queryHistory = (ListView) findViewById(R.id.lv_pegging_result); loadMoreView = LayoutInflater.from(this).inflate(R.layout.load_more, null); headView = LayoutInflater.from(this).inflate(R.layout.peg_res_head, null); ll_headline = (LinearLayout) headView.findViewById(R.id.ll_headline); ll_headline.setVisibility(View.GONE); queDomain = (TextView) headView.findViewById(R.id.tv_query_domain); queDate = (TextView) headView.findViewById(R.id.tv_query_date); queDomain.setText("查询的域名"); queDate.setText("查询时间"); loadMoreButton = (Button) loadMoreView.findViewById(R.id.btn_loading); lv_queryHistory.addFooterView(loadMoreView); lv_queryHistory.addHeaderView(headView); lv_queryHistory.setOnItemClickListener(listItemClick); } private void initData() {//初始化数据 lstHistories = ManageHistory.getLimitHistory(count, Global.PAGESIZE); if(lstHistories==null){ if (sumHistory >= ManageHistory.getAllHistory().size()) { loadMoreButton.setVisibility(View.GONE); } }else{ sumHistory = lstHistories.size(); peggingResAdapter = new PeggingResultAdapter(this.getApplication(), lstHistories, null); lv_queryHistory.setAdapter(peggingResAdapter); if (sumHistory >= ManageHistory.getAllHistory().size()) { loadMoreButton.setVisibility(View.GONE); } } } public void loadMore(View view) {//单击事件 dialog.showProgressDialog(QueryHistoryActivity.this, "数据加载中", true); loadMoreButton.setText("加载数据中..."); // 设置按钮文字loading loadData(); peggingResAdapter.notifyDataSetChanged(); // 数据集变化后,通知adapter // // listView.setSelection(visibleLastIndex - visibleItemCount + // // 1); // 设置选中项 loadMoreButton.setText("点击加载更多"); // 恢复按钮文字 dialog.dismissProgressDialog(); } /** * * 加载更多的数据 * */ private void loadData() {//加载数据 count += 1; lstHistories = ManageHistory.getLimitHistory(count * Global.PAGESIZE, Global.PAGESIZE); sumHistory += lstHistories.size(); for (int i = 0; i < lstHistories.size(); i++) { peggingResAdapter.AddItem(lstHistories.get(i)); } if (sumHistory >= ManageHistory.getAllHistory().size()) { loadMoreButton.setVisibility(View.GONE); Toast.makeText(getApplicationContext(), "没有更多的数据了", Toast.LENGTH_LONG).show(); } }
适配器ListView
package cn.ename.cxw.whois.adapter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import cn.ename.cxw.R; import cn.ename.cxw.whois.domain.InquireHistory; import cn.ename.cxw.whois.domain.PostInformations; import cn.ename.cxw.whois.domain.WhoisData; import cn.ename.cxw.whois.net.StringUtils; public class PeggingResultAdapter extends BaseAdapter { private Context mContext; private TextView domain; private TextView resultDate; private List<InquireHistory> lstHistory; public PeggingResultAdapter(Context mContext) { super(); this.mContext = mContext; } public PeggingResultAdapter(Context mContext, List<InquireHistory> lstHistory, String histroy) { super(); this.mContext = mContext; this.lstHistory = lstHistory; } @Override public int getCount() { // TODO Auto-generated method stub return this.lstHistory.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = LayoutInflater.from(mContext).inflate(R.layout.pegging_result_item, null); domain = (TextView) convertView.findViewById(R.id.tv_domain); resultDate = (TextView) convertView.findViewById(R.id.tv_reg_date); domain.setText(lstHistory.get(position).getDomain()); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd").parse(lstHistory.get(position).getSaveTime()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } String now = new SimpleDateFormat("yyyy-MM-dd").format(date); resultDate.setText(now); return convertView; } public void AddItem(InquireHistory inquireHistory) { this.lstHistory.add(inquireHistory); Log.i("SUM", inquireHistory.toString()); } }
peg_res_head.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white" android:orientation="vertical" > <LinearLayout android:id="@+id/ll_headline" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal" > <TextView android:id="@+id/tv_hl_domain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="baidu.com" android:textSize="15sp" android:textColor="@color/black" /> </LinearLayout> <LinearLayout android:id="@+id/ll_query_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/gray_nav" android:orientation="horizontal" android:padding="10dp" > <TextView android:id="@+id/tv_query_domain" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3" android:paddingRight="10dp" android:singleLine="true" android:text="域名" android:ellipsize="end" android:textSize="15sp" android:textColor="@color/black" /> <TextView android:id="@+id/tv_query_date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="5" android:text="注册时间" android:textSize="15sp" android:textColor="@color/black" /> </LinearLayout> </LinearLayout>
load_more.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn_loading" android:layout_width="fill_parent" android:layout_height="50dp" android:text="点击加载更多" android:onClick="loadMore" android:background="@color/transparent" /> </LinearLayout>
效果图