2017-06-12 16 views
0

LoadManagerを使用してリストビューにアイテムを追加しようとしています。しかし、私のリストビューが更新されるたびに、私は古い結果を見ることができません。すべての結果を確認し、新しい部分を下部にロードするにはどうすればよいですか? listviewに複数の項目を読み込むに対処するためのLoaderManagerを使用してListViewにアイテムを追加する方法

private OnScrollListener onScrollListener() { 
return new OnScrollListener() { 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     int threshold = 1; 
     int count = listView.getCount(); 

     if (scrollState == SCROLL_STATE_IDLE) { 
      // is it good condition to load new data dynamically? 
      if (listView.getLastVisiblePosition() >= count - threshold) { 

       // startIndex is a start point for the query 
       // default value is 0 
       startIndex += 10; 
       // this method format the URL using URI.Builder and change startIndex 
       formatUrl(false, keywords, startIndex); 

       // Clear the adapter 
       mAdapter.clear(); 

       // I think the problem is here?! But how to fix it?! 
       getLoaderManager().restartLoader(LOADER_ID, null, MainActivity.this); 
      } 
     } 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, 
         int totalItemCount) { 
    } 
}; 

答えて

0

良いアプローチがある:

  • ユーザーがlistviewの底に到達するたびに、あなたのアイテムリストにアイテムをロードloadMoreItems()関数を呼び出します。

  • notifyDataSetChanged()の方法を使用して、adapterに項目リストが変更されたことを通知します。

アイテムローディング関数の単純な実装:

public loadMoreItems() 
    { 
     // your network logic here 

     //append your items to your list 
     for(Item item : fetchedItems) 
     { 
     items.add(item); 
     } 

     //inform the adapter about the data change 
     adapter.notifyDataSetChanged(); 

    } 
関連する問題