在使用ListView时,会遇到当ListView列表滑动到最底端时,添加新的列表项的问题,本文通过代码演示如何动态的添加新的列表项到ListView中。
实现步骤:调用ListView的setOnScrollListener()方法设置滑动监听器,实现OnScrollListener接口的方法,判断当列表滑动到最低端时,加载新的列表项。
其中OnScrollListener接口需要实现如下两个方法:
onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
view 报告滑动状态的视图
firstVisibleItem 可视的第一个列表项的索引
visibleItemCount 可视的列表项个数
totalItemCount 总共的列表项个数
onScrollStateChanged(AbsListView view, int scrollState)
view 报告滑动状态的视图
scrollState 滑动状态
滑动状态包括
SCROLL_STATE_IDLE : 0 视图没有滑动
SCROLL_STATE_TOUCH_SCROLL : 1 用户正在触摸滑动,手指仍在屏幕上
SCROLL_STATE_FLING : 2 用户之前触摸滑动,现在正在滑行,直到停止
下面通过代码给大家演示效果
main.xml
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
3 | android:orientation = "vertical" |
4 | android:layout_width = "fill_parent" |
5 | android:layout_height = "fill_parent" |
8 | android:id = "@id/android:list" |
9 | android:layout_width = "match_parent" |
10 | android:layout_height = "match_parent" |
11 | android:layout_weight = "1" |
12 | android:drawSelectorOnTop = "false" |
footer.xml
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
3 | android:layout_width = "fill_parent" |
4 | android:layout_height = "wrap_content" |
5 | android:gravity = "center" |
6 | android:orientation = "horizontal" |
9 | android:id = "@+id/progressbar" |
10 | android:layout_width = "wrap_content" |
11 | android:layout_height = "wrap_content" |
12 | style = "\?android:attr/progressBarStyleSmall" |
15 | android:layout_width = "wrap_content" |
16 | android:layout_height = "wrap_content" |
17 | android:textSize = "20.0sp" |
18 | android:text = "正在加载..." |
MainActicity.xml
1 | package com.szy.listview; |
3 | import android.app.ListActivity; |
4 | import android.os.Bundle; |
5 | import android.os.Handler; |
6 | import android.os.Message; |
7 | import android.view.Gravity; |
8 | import android.view.View; |
9 | import android.view.ViewGroup; |
10 | import android.widget.AbsListView; |
11 | import android.widget.BaseAdapter; |
12 | import android.widget.ListView; |
13 | import android.widget.TextView; |
20 | public class MainActivity extends ListActivity |
22 | private ListView mListView; |
23 | private CustomAdapter mAdapter; |
24 | private int mScrollState; |
28 | public void onCreate(Bundle savedInstanceState) |
30 | super .onCreate(savedInstanceState); |
31 | setContentView(R.layout.main); |
34 | mListView = getListView(); |
35 | //根据footer.xml描述创建View |
36 | mFooter = getLayoutInflater().inflate(R.layout.footer, null ); |
37 | // 在ListView底部添加正在加载视图 |
38 | //注意:addFooterView方法需要在调用setListAdapter方法前调用! |
39 | mListView.addFooterView(mFooter); |
40 | mAdapter = new CustomAdapter(); |
41 | setListAdapter(mAdapter); |
43 | mListView.setOnScrollListener( new AbsListView.OnScrollListener() |
46 | public void onScrollStateChanged(AbsListView view, int scrollState) |
49 | mScrollState = scrollState; |
53 | public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) |
56 | int lastVisibleItem = firstVisibleItem + visibleItemCount - 1 ; |
57 | //当列表正处于滑动状态且滑动到列表底部时,执行 |
58 | if (mScrollState != AbsListView.OnScrollListener.SCROLL_STATE_IDLE |
59 | && lastVisibleItem == totalItemCount - 1 ) |
61 | // 执行线程,模拟睡眠5秒钟后添加10个列表项 |
65 | private Handler handler = new Handler() |
69 | public void handleMessage(Message msg) |
71 | super .handleMessage(msg); |
75 | mAdapter.notifyDataSetChanged(); |
87 | handler.sendEmptyMessage( 0 ); |
88 | } catch (InterruptedException e) |
100 | private class CustomAdapter extends BaseAdapter |
106 | public int getCount() |
112 | public Object getItem( int position) |
118 | public long getItemId( int position) |
124 | public View getView( int position, View convertView, ViewGroup parent) |
126 | TextView result = (TextView) convertView; |
127 | //动态创建TextView添加早ListView中 |
130 | result = new TextView(MainActivity. this ); |
131 | result.setTextAppearance(MainActivity. this , android.R.style.TextAppearance_Large); |
132 | AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams |
133 | (AbsListView.LayoutParams.FILL_PARENT, |
134 | AbsListView.LayoutParams.WRAP_CONTENT); |
135 | result.setLayoutParams(layoutParams); |
136 | result.setGravity(Gravity.CENTER); |
138 | result.setText( "第 " + (position + 1 )+ "行" ); |