2014-12-04 67 views
0

我是Android新手。我的要求是加載額外的數據&設置到列表視圖,當滾動到達底部。我可以調用下一個數據,但我以前的數據是隱藏的。如何糾正?滾動達到底部時加載更多數據

listView.setOnScrollListener(new OnScrollListener() { 

       @Override 
       public void onScrollStateChanged(AbsListView view, 
         int scrollState) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onScroll(AbsListView view, 
         int firstVisibleItem, int visibleItemCount, 
         int totalItemCount) { 
        // TODO Auto-generated method stub 
        if (listView.getLastVisiblePosition() == listView 
          .getAdapter().getCount() - 1 
          && listView.getChildAt(
            listView.getChildCount() - 1) 
            .getBottom() <= listView 
            .getHeight()) { 
         // scroll end reached 

         if (nextCall == null || nextCall == "" 
           || nextCall.equals("null") 
           || nextCall.equals("")) { 

         } else { 

          hostname=nextCall; 

          new ArticlesList().execute(); 

         } 

         // Write your code here 
        }else{ 

        } 

       } 
      }); 
      dialog.dismiss(); 
     } else { 

      Toast.makeText(getActivity(), "Went wrong", 
        Toast.LENGTH_LONG).show(); 
      dialog.dismiss(); 
     } 
+0

考慮使用'ScrollView'而不是'ListView',它是'onScrollChanged'方法來識別何時到達ScrollView的底部。 – 2014-12-04 08:40:49

回答

3

訪問這是描述如何在列表視圖中進行分頁。 PagingListView

1

ListView中適配器:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    if (position == items.size() - 1) 
    { 
     items.add("new data1"); 
     items.add("new data2"); 
     notificationDataSetChanged(); 
    } 

    //inject your view here 

} 

你失去了你的數據,因爲你重寫鏈接。您應該添加數據,而不是替換它。

+0

當我嘗試像這樣(position == items.size() - 1),在底部滾動到達之前,它調用 – 2014-12-04 10:22:10

+0

@madhusudhan sry,我不能理解你(請用另一個字:) :) – Tooto 2014-12-05 08:39:19

1

你需要的是一個永無止境的滾動監聽器,請看看這個:https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews

這個項目只是添加到你的,這樣使用它:

yourListView.setOnScrollListener(new EndlessScrollListener() { 
    @Override 
    public void onLoadMore(int page, int totalItemsCount) { 

     ... your asynchTask 
    } 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     super.onScrollStateChanged(view, scrollState); 
    } 
}); 
+0

thanq Milad我知道了 – 2014-12-05 07:21:35

0

我使用解決同樣的問題以下代碼片段。

private int lastVisibleItem;私人LinearLayoutManager linearLayoutManager; private int lastId; //在第一次從服務器加載數據後,獲取您帖子的最後一個ID。 private int flag = 0;

linearLayoutManager = new LinearLayoutManager(getActivity());

recyclerView_news.setLayoutManager(linearLayoutManager);

recyclerView_news.addOnScrollListener(新RecyclerView.OnScrollListener(){

@Override 
public void onScrollStateChanged(RecyclerView recyclerView, int newState){ 
    super.onScrollStateChanged(recyclerView, newState); 
} 

@Override 
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
    super.onScrolled(recyclerView, dx, dy); 
    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); 

    new Handler().post(new Runnable() { 
     @Override 
     public void run() { 
      if (list_news.size() - 1 == lastVisibleItem) { 
       if (flag == 0) { 
        flag = 1; 
        getMoreData(lastId); // pass lastId to load next posts. 
       } 
      } else if (list_news.size() - 1 != lastVisibleItem) { 
       flag = 0; 
      } 
     } 
    }); 
} 

}); 希望這會有所幫助。謝謝。

相關問題