精確監聽 Android ListView 滑動到底部

jopen 10年前發布 | 70K 次閱讀 Android Android開發 移動開發

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>  


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>  


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) {  

        }  

    }  

}  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!