ListView的分页加载功能
参考资料: 首先新建一个layout文件,命名为moredata.xml,包含一个Button和一个ProgressBar,xml代码如下:然后定义一个item.xml,其作用是定义ListView的每个item的视图,代码如下: 下面是主MainActivity.java代码: package cn.llbb.testlistview2; import android.support.v7.app.ActionBarActivity; import java.util.*; import android.os.Bundle; import android.os.Handler; import android.view.*; import android.view.View.*; import android.widget.*; import android.widget.AbsListView.OnScrollListener; public class MainActivity extends ActionBarActivity implements OnScrollListener{ private ListView list = null; private SimpleAdapter mSimpleAdapter; private Button bt; private ProgressBar pg; private Handler handler; private ArrayList > listitem; private View moreView; private int MaxDateNum; private int lastVisibleIndex; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = (ListView) findViewById(R.id.listView1); MaxDateNum = 65; listitem = new ArrayList >(); moreView = getLayoutInflater().inflate(R.layout.moredata, null); bt = (Button) moreView.findViewById(R.id.bt_load); pg = (ProgressBar) moreView.findViewById(R.id.pg); handler = new Handler(); listitem = new ArrayList >(); for(int i = 0;i < 10; i++){ HashMap item = new HashMap (); item.put("ItemTitle", "第" + i + "行标题"); item.put("ItemText", "第" + i + "行内容"); listitem.add(item); } mSimpleAdapter = new SimpleAdapter(this, listitem, R.layout.item, new String[]{"ItemTitle","ItemText"}, new int[]{R.id.tv_title,R.id.tv_content}); list.addFooterView(moreView); list.setAdapter(mSimpleAdapter); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { pg.setVisibility(View.VISIBLE); bt.setVisibility(View.GONE); handler.postDelayed(new Runnable() { @Override public void run() { loadMoreDate();// 加载更多数据 bt.setVisibility(View.VISIBLE); pg.setVisibility(View.GONE); mSimpleAdapter.notifyDataSetChanged();// 通知listView刷新数据 } }, 2000); } }); } private void loadMoreDate() { int count = mSimpleAdapter.getCount(); if (count + 5 < MaxDateNum) { // 每次加载5条 for (int i = count; i < count + 5; i++) { HashMap map = new HashMap (); map.put("ItemTitle", "新增第" + i + "行标题"); map.put("ItemText", "新增第" + i + "行内容"); listitem.add(map); } } else { // 数据已经不足5条 for (int i = count; i < MaxDateNum; i++) { HashMap map = new HashMap (); map.put("ItemTitle", "新增第" + i + "行标题"); map.put("ItemText", "新增第" + i + "行内容"); listitem.add(map); } } } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { lastVisibleIndex = firstVisibleItem + visibleItemCount - 1; if (totalItemCount == MaxDateNum + 1) { list.removeFooterView(moreView); Toast.makeText(this, "数据全部加载完成,没有更多数据!", Toast.LENGTH_LONG).show(); } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && lastVisibleIndex == mSimpleAdapter.getCount()) { } } }
当前文章:ListView的分页加载功能
当前地址:http://scyanting.com/article/jphssd.html