Android Endless List

后端 未结 10 1168
梦如初夏
梦如初夏 2020-11-22 02:52

How can I create a list where when you reach the end of the list I am notified so I can load more items?

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:34

    May be a little late but the following solution happened very useful in my case. In a way all you need to do is add to your ListView a Footer and create for it addOnLayoutChangeListener.

    http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)

    For example:

    ListView listView1 = (ListView) v.findViewById(R.id.dialogsList); // Your listView
    View loadMoreView = getActivity().getLayoutInflater().inflate(R.layout.list_load_more, null); // Getting your layout of FooterView, which will always be at the bottom of your listview. E.g. you may place on it the ProgressBar or leave it empty-layout.
    listView1.addFooterView(loadMoreView); // Adding your View to your listview 
    
    ...
    
    loadMoreView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
             Log.d("Hey!", "Your list has reached bottom");
        }
    });
    

    This event fires once when a footer becomes visible and works like a charm.

提交回复
热议问题