哎,项目需要listview下拉加载更多写个博客就当总结!
思路:在listview底部加一个布局,加载数据的时候有这个布局,当数据加载完成就没有了。
1、addFooterView()方法可以加一个布局。
2、setVisibility()这个方法控制布局是出现啊,还是消失(隐藏)。
不多说上代码:首先一个显示一个listvie这个各位先准备我就不写了
底部的一布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <LinearLayout android:id="@+id/loading" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <ProgressBar android:id="@+id/footprogress" style="?android:progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_gravity="center_vertical" android:indeterminateBehavior="repeat" /> <!-- 数据 --> <TextView android:id="@+id/tv_scro" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left|center" android:padding="3.0px" android:text="读取中..." android:textColor="#ff000000" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
先定义几个全局变量:(在项目就不慢慢弄了)
private View loging; private int addlist = 0; private int iii = 1;
/** * 分页加载 */ loging = LayoutInflater.from(getActivity()) .inflate(R.layout.scro, null); loging.findViewById(R.id.loading).setVisibility(View.GONE); listView.addFooterView(loging); listView.setOnScrollListener(new OnScrollListener() { private int totalItemCounts;// 总数量; private int lastVisibleItem;// 最后一个可见的item; @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.totalItemCounts = totalItemCount; this.lastVisibleItem = firstVisibleItem + visibleItemCount; } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { is_divPage = true; if (is_divPage = true && totalItemCounts == lastVisibleItem && scrollState == OnScrollListener.SCROLL_STATE_IDLE) { loging.findViewById(R.id.loading).setVisibility( View.VISIBLE); addlistview(); } } });
开启一个线程获得数据 * 获取下拉的数据 */ private void addlistview() { new Thread() { public void run() { try { iii++; companiesLists = GetWebData .getCompany("http://jy.hsei.org:8086/UnitBaseInfo/GetAllUnit/" + iii + "/10"); if (companiesLists != null) { addlist = addlist + companiesLists.size(); } companiesList.addAll(companiesLists); Message msg = Message.obtain(); msg.obj = companiesList; msg.what = 1; handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } }; }.start();
说一下上面分页的代码:把一个布局加到listview底部,但是给隐藏掉,如果listview 的setOnScrollListener事件里面触发了的话,那么显示这个(加载)布局同时访问网络获得数据。
有了数据还要把获得的数据放到最开始的数据集合(addAll())里面然后让listview一起显示出来
listview里面显示出来:
/* * 网络访问 */ handler = new Handler() { @SuppressWarnings("unchecked") @Override public void handleMessage(Message msg) { companiesList = (ArrayList<Company>) msg.obj; if (companiesList != null) { datas = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < companiesList.size(); i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("UnitTypeID", companiesList.get(i).UnitTypeID); map.put("UnitCode", companiesList.get(i).UnitCode); map.put("UnitName", companiesList.get(i).UnitName); map.put("Address", companiesList.get(i).Address); map.put("UnitID", companiesList.get(i).UnitID); datas.add(map); } // System.out.println("获取的1单位数据:" + data.toString()); if (datas != null) { progressDialog.dismiss(); } simpleAdapter = new SimpleAdapter(getActivity(), datas, R.layout.item_units, new String[] { "UnitTypeID", "UnitCode", "UnitName", "Address" }, new int[] { R.id.tv_item_UnitType, R.id.tv_item_UnitCode, R.id.tv_item_UnitName, R.id.tv_item_UnitAddress }); listView.setAdapter(simpleAdapter); loging.findViewById(R.id.loading).setVisibility(View.GONE); if (addlist > 0) { listView.setSelection(addlist - 1); simpleAdapter.notifyDataSetChanged();// // 通知adapter数据有变化 } } else { datas.clear(); simpleAdapter.notifyDataSetChanged(); Toast.makeText(getActivity(), "没有找到您要的数据", Toast.LENGTH_SHORT).show(); } } };
上面的代码就是把从网络上获取到的数据先放在listview里面了,然后在把(加载)布局给隐藏掉就这样完成了下拉加载更多的效果。
这个是listview加载更多源码