Android中动态更新ListView【附源码】
在使用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 用户之前触摸滑动,现在正在滑行,直到停止
下面通过代码给大家演示效果
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ListView
- android:id="@id/android:list"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"
- />
- </LinearLayout>
// footer.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="wrap_content"
- android:gravity="center"
- android:orientation="horizontal"
- >
- <ProgressBar
- android:id="@+id/progressbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="\?android:attr/progressBarStyleSmall"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20.0sp"
- android:text="正在加载..."
- />
- </LinearLayout>
然后是主代码:
- public class MainActivity extends ListActivity
- {
- private ListView mListView;
- private CustomAdapter mAdapter;
- private int mScrollState;
- private View mFooter;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //获取ListView
- mListView = getListView();
- //根据footer.xml描述创建View
- mFooter = getLayoutInflater().inflate(R.layout.footer, null);
- // 在ListView底部添加正在加载视图
- //注意:addFooterView方法需要在调用setListAdapter方法前调用!
- mListView.addFooterView(mFooter);
- mAdapter = new CustomAdapter();
- setListAdapter(mAdapter);
- //给ListView添加滚动监听器
- mListView.setOnScrollListener(new AbsListView.OnScrollListener()
- {
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState)
- {
- //记录当前状态
- mScrollState = scrollState;
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
- {
- // 可视的最后一个列表项的索引
- int lastVisibleItem = firstVisibleItem + visibleItemCount - 1;
- //当列表正处于滑动状态且滑动到列表底部时,执行
- if (mScrollState != AbsListView.OnScrollListener.SCROLL_STATE_IDLE
- && lastVisibleItem == totalItemCount - 1)
- {
- // 执行线程,模拟睡眠5秒钟后添加10个列表项
- new Thread()
- {
- private Handler handler = new Handler()
- {
- @Override
- public void handleMessage(Message msg)
- {
- super.handleMessage(msg);
- //增加Item数量
- mAdapter.count += 10;
- //通知数据集变化
- mAdapter.notifyDataSetChanged();
- }
- };
- @Override
- public void run()
- {
- super.run();
- try
- {
- sleep(5000);
- handler.sendEmptyMessage(0);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }.start();
- }
- }
- });
- }
- private class CustomAdapter extends BaseAdapter
- {
- // 初始列表项数量
- int count = 20;
- @Override
- public int getCount()
- {
- return count;
- }
- @Override
- public Object getItem(int position)
- {
- return position;
- }
- @Override
- public long getItemId(int position)
- {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent)
- {
- TextView result = (TextView) convertView;
- //动态创建TextView添加早ListView中
- if (result == null)
- {
- result = new TextView(MainActivity.this);
- result.setTextAppearance(MainActivity.this, android.R.style.TextAppearance_Large);
- AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams
- (AbsListView.LayoutParams.FILL_PARENT,
- AbsListView.LayoutParams.WRAP_CONTENT);
- result.setLayoutParams(layoutParams);
- result.setGravity(Gravity.CENTER);
- }
- result.setText("第 " + (position + 1)+"行");
- return result;
- }
- }
- }
效果预览:
Android中动态更新ListView源码下载
具体下载目录在 /2012年资料/8月/4日/Android中动态更新ListView【附源码】/