使用ListView过程中,如果滚动加载数据的操作比较费时,很容易在滚屏时出现屏幕卡住的现象,一个解决的办法就是不要在滚动时加载数据,而是等到滚动停止后再进行数据的加载。这同样要实现OnScrollListener接口,关于该接口的简要描述见上一篇文章,这里直接进行代码的分析:
- package hust.iprai.asce1885;
-
- import android.app.ListActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
-
- public class MainActivity extends ListActivity implements OnScrollListener {
-
- private TextView mStatus;
- private boolean mBusy = false;
-
-
- private class SlowAdapter extends BaseAdapter {
- private LayoutInflater mInflater;
-
- public SlowAdapter(Context context) {
- mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
-
-
- public int getCount() {
- return mStrings.length;
- }
-
-
- public Object getItem(int position) {
- return position;
- }
-
-
- public long getItemId(int position) {
- return position;
- }
-
-
- public View getView(int position, View convertView, ViewGroup parent) {
- TextView text;
-
-
- if (null == convertView) {
- text = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
- } else {
- text = (TextView) convertView;
- }
-
- if (!mBusy) {
-
- text.setText(mStrings[position]);
-
- text.setTag(null);
- } else {
-
- text.setText("Loading...");
-
- text.setTag(this);
- }
-
- return text;
- }
-
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mStatus = (TextView) findViewById(R.id.status);
- mStatus.setText("Idle");
-
-
- setListAdapter(new SlowAdapter(this));
-
-
- getListView().setOnScrollListener(this);
- }
-
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
-
- }
-
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- switch (scrollState) {
- case OnScrollListener.SCROLL_STATE_IDLE:
- mBusy = false;
-
- int first = view.getFirstVisiblePosition();
- int count = view.getChildCount();
- for (int i = 0; i < count; i++) {
- TextView tv = (TextView) view.getChildAt(i);
- if (tv.getTag() != null) {
- tv.setText(mStrings[first + i]);
- tv.setTag(null);
- }
- }
-
- mStatus.setText("Idle");
- break;
- case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
- mBusy = true;
- mStatus.setText("Touch Scroll");
- break;
- case OnScrollListener.SCROLL_STATE_FLING:
- mBusy = true;
- mStatus.setText("Fling");
- break;
- default:
- mStatus.setText("Are you kidding me!");
- break;
- }
- }
-
- private String[] mStrings = {
- "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
- "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
- "Afuega‘l Pitu", "Airag", "Airedale", "Aisy Cendre",
- "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
- "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh",
- "Anthoriro", "Appenzell", "Aragon", "Ardi Gasna", "Ardrahan",
- "Armenian String", "Aromes au Gene de Marc", "Asadero", "Asiago",
- "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel",
- "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal",
- "Banon", "Barry‘s Bay Cheddar", "Basing", "Basket Cheese",
- "Bath Cheese", "Bavarian Bergkase", "Baylough", "Beaufort",
- "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese",
- "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir",
- "Bierkase", "Bishop Kennedy", "Blarney", "Bleu d‘Auvergne",
- "Bleu de Gex", "Bleu de Laqueuille", "Bleu de Septmoncel",
- "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore",
- "Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini",
- "Bocconcini (Australian)", "Boeren Leidenkaas", "Bonchester",
- "Bosworth"};
-
-
- }
下面是布局文件main.xml:
- <?xml version="1.0" encoding="utf-8"?>
-
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <ListView android:id="@android:id/list"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"/>
-
- <TextView android:id="@+id/status"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="8dip"
- android:paddingRight="8dip"/>
-
- </LinearLayout>
程序运行结果如下图所示:

Android开源代码解读のOnScrollListener实现ListView滚屏时不加载数据
原文:http://www.cnblogs.com/dongweiq/p/4290220.html