精确监听 Android ListView 滑动到底部

jopen 11年前

main.xml如下:

    <RelativeLayout             xmlns:android="http://schemas.android.com/apk/res/android"            xmlns:tools="http://schemas.android.com/tools"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            >                    <ListView                android:id="@+id/listView"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_centerInParent="true"             />                </RelativeLayout>  
</div> </div>


listviewitem.mxl如下:

    <?xml version="1.0" encoding="utf-8"?>        <RelativeLayout             xmlns:android="http://schemas.android.com/apk/res/android"            android:layout_width="fill_parent"            android:layout_height="fill_parent" >                    <TextView                android:id="@+id/textView"                android:layout_width="fill_parent"                android:gravity="center"                android:layout_height="80dip"             />                </RelativeLayout>  
</div> </div>


MainActivity如下:

import java.util.ArrayList;    import java.util.HashMap;    import android.app.Activity;    import android.os.Bundle;    import android.view.View;    import android.widget.AbsListView;    import android.widget.AbsListView.OnScrollListener;    import android.widget.ListView;    import android.widget.SimpleAdapter;        public class MainActivity extends Activity {        private ListView mListView;        private SimpleAdapter mSimpleAdapter;        private HashMap<String, Object> mHashMap;        private ArrayList<HashMap<String, Object>> mArrayList;                @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.main);            init();        }        private void init(){            mListView=(ListView) findViewById(R.id.listView);            mArrayList=new ArrayList<HashMap<String,Object>>();            addDataForListView();            mSimpleAdapter=new SimpleAdapter           (MainActivity.this, mArrayList, R.layout.listviewitem, new String []{"id"},new int []{R.id.textView});            mListView.setAdapter(mSimpleAdapter);            mListView.setOnScrollListener(new OnScrollListenerImple());                    }        private void addDataForListView(){            System.out.println("====>Add data to ListView");            for (int i = 0; i < 10; i++) {                mHashMap=new HashMap<String, Object>();                mHashMap.put("id", ""+i);                mArrayList.add(mHashMap);            }        }        private class OnScrollListenerImple implements OnScrollListener{            @Override            public void onScroll(AbsListView listView, int firstVisibleItem,int visibleItemCount, int totalItemCount) {                int lastItem = firstVisibleItem + visibleItemCount;                if(lastItem == totalItemCount) {                    System.out.println("Scroll to the listview last item");                    View lastItemView=(View) listView.getChildAt(listView.getChildCount()-1);                    if ((listView.getBottom())==lastItemView.getBottom()) {                        System.out.println("========Scroll to the listview bottom =============");                        addDataForListView();                        mSimpleAdapter.notifyDataSetChanged();                    }                }            }                @Override            public void onScrollStateChanged(AbsListView listview, int scrollState) {                            }                    }        }  
</div> </div>