Endless scrolling listview not working

前端 未结 2 606
小鲜肉
小鲜肉 2020-11-30 09:19

I am developing one application. In my app I am using listview to display data from json. All the data display perfectly. But what I want is, to display first 10 objects and

相关标签:
2条回答
  • 2020-11-30 09:59

    To have an AdapterView (such as a ListView or GridView) that automatically loads more items as the user scrolls through the items (aka infinite scroll). This is done by triggering a request for more data once the user crosses a threshold of remaining items before they've hit the end. Every AdapterView has support for binding to the OnScrollListener events which are triggered whenever a user scrolls through the collection. Using this system, we can define a basic EndlessScrollListener which supports most use cases by creating our own class that extends OnScrollListener.

    You can find more about this in the below links :

    Endless Scrolling ListView in Android

    Endless Scrolling with AdapterViews

    Infinite Scrolling List View

    A simpler approach of adding data in a listview has been show in this SO answer: Android Endless List

    Hope this helps ...

    0 讨论(0)
  • 2020-11-30 10:12

    To Show only 10 item first time in ListView and show more items on scroll follow following steps:

    STEP 1: Create a chunkList method which divide ArrayList in parts of size 10:

    static <T> List<ArrayList<T>> chunkList(List<T> list, final int L) {
            List<ArrayList<T>> parts = new ArrayList<ArrayList<T>>();
            final int N = list.size();
            for (int i = 0; i < N; i += L) {
                parts.add(new ArrayList<T>(
                    list.subList(i, Math.min(N, i + L)))
                );
            }
            return parts;
        }
    

    STEP 2: Create ArrayList and a counter which show current part :

        private boolean isLoadingMore=false;
        Handler mHandler = new Handler();
        List<ArrayList<HashMap<String,String>>> mainArrayList;
        private int count=0;
    

    STEP 3: In onPostExecute break result and show data in ListView:

    protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
    
                  super.onPostExecute(result);
                  // your code here...
                   mainArrayList=chunkList(result,10);
                   isLoadingMore=false;
                   count=0;
                   adapter = new CustomAdapterSent(Interestsent.this, 
                                                mainArrayList.get(count));
                   setListAdapter(adapter);
            }
    
        }
    

    STEP 4: Create addAll method in Adapter to append data in current data source :

    public void addAll(ArrayList<HashMap<String,String>> moreData){
       this.listData.addAll(moreData);
       this.notifyDataSetChanged();
    }
    

    STEP 5: In onLoadMore load more data in ListView:

    mHandler = new Handler();
     listview.setOnScrollListener(new EndlessScrollListener() {
            @Override
            public void onLoadMore(int page, int totalItemsCount) {
                     if(count<mainArrayList.size()-1)
                      {
                         if(adapter !=null){
                count++;                       
                if(!isLoadingMore){
                                 isLoadingMore=true;
                                 mHandler.postDelayed(loadMoreRunnable,1000); 
                              } 
                          }
                      } 
            }
            });
        }
    
         Runnable loadMoreRunnable = new Runnable() {
    
            @Override
            public void run() {
                // TODO Auto-generated method stub
             if(count<mainArrayList.size())
                       {
                         if(adapter !=null){
                          count++;                       
                          adapter.addAll(mainArrayList.get(count));
                          mHandler.removeCallbacks(loadMoreRunnable);
                          isLoadingMore=false;
                          }
                      } 
            }
        };
    
    0 讨论(0)
提交回复
热议问题