Android Endless List

后端 未结 10 1162
梦如初夏
梦如初夏 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条回答
  • 2020-11-22 03:32

    At Ognyan Bankov GitHub i found a simple and working solution!

    It makes use of the Volley HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository.

    The given code demonstrates:

    1. ListView which is populated by HTTP paginated requests.
    2. Usage of NetworkImageView.
    3. "Endless" ListView pagination with read-ahead.

    For future consistence i forked Bankov's repo.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 03:39

    The key of this problem is to detect the load-more event, start an async request for data and then update the list. Also an adapter with loading indicator and other decorators is needed. In fact, the problem is very complicated in some corner cases. Just a OnScrollListener implementation is not enough, because sometimes the items do not fill the screen.

    I have written a personal package which support endless list for RecyclerView, and also provide a async loader implementation AutoPagerFragment which makes it very easy to get data from a multi-page source. It can load any page you want into a RecyclerView on a custom event, not only the next page.

    Here is the address: https://github.com/SphiaTower/AutoPagerRecyclerManager

    0 讨论(0)
  • 2020-11-22 03:43

    I've been working in another solution very similar to that, but, I am using a footerView to give the possibility to the user download more elements clicking the footerView, I am using a "menu" which is shown above the ListView and in the bottom of the parent view, this "menu" hides the bottom of the ListView, so, when the listView is scrolling the menu disappear and when scroll state is idle, the menu appear again, but when the user scrolls to the end of the listView, I "ask" to know if the footerView is shown in that case, the menu doesn't appear and the user can see the footerView to load more content. Here the code:

    Regards.

    listView.setOnScrollListener(new OnScrollListener() {
    
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            if(scrollState == SCROLL_STATE_IDLE) {
                if(footerView.isShown()) {
                    bottomView.setVisibility(LinearLayout.INVISIBLE);
                } else {
                    bottomView.setVisibility(LinearLayout.VISIBLE);
                } else {
                    bottomView.setVisibility(LinearLayout.INVISIBLE);
                }
            }
        }
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题