2012-08-29 64 views
0

我需要幫助才能使我的Gridview OnscrollListener正常工作。我的結果沒有按預期運作。它只是加載我所有的gridview項目,然後當我將gridview滾動到底部時,它顯示一個進度對話框並刷新。Gridview OnScrollListener無法正常工作

我承認我不理解OnScrollListener()中的邏輯。我指的是Endless Scrolling ListView in Android這個例子。希望有人可以爲我編輯代碼?

我想限制每個滾動20個圖像,直到完成從我的XML加載所有我的圖像。我從xml解析URL,然後加載到我的Gridview中。

GridViewActivity.class

public class GridViewActivity extends Activity { 
     static final String URL = "http://api.androidhive.info/music/music.xml"; 
     private ProgressDialog pDialog; 
     ArrayList<HashMap<String, String>> songsList; 
     static final String KEY_SONG = "song"; 
     static final String KEY_ID = "id"; 
     static final String KEY_CAT_ARTIST = "artistcat"; 
     static final String KEY_THUMB_URL = "thumb_url"; 
     static final String KEY_BIG_URL = "big_url"; 
     static final String KEY_CAT_URL = "cat_url"; 
     GridView grid; 
     GridViewActivityLazyAdapter adapter; 
     String cat_url; 
     String artist_url; 
     private int visibleThreshold = 5; 
     private int currentPage = 0; 
     private int previousTotal = 0; 
     private boolean loading = true; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.gridview_main); 

      new loadGridView().execute(); 

      grid = (GridView) findViewById(R.id.grid_view); 

     grid.setOnScrollListener(new OnScrollListener() { 

     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, 
       int visibleItemCount, int totalItemCount) { 
      // TODO Auto-generated method stub 
      if (loading) { 
       if (totalItemCount > previousTotal) { 
        loading = false; 
        previousTotal = totalItemCount; 
        currentPage++; 
       } 
      } 
      if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 
       // I load the next page of gigs using a background task, 
       // but you can call any function here. 
       new loadGridView().execute(currentPage + 20); 
       loading = true; 
      } 
     } 

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

    } 

     public class loadGridView extends AsyncTask<Integer, String, String> { 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       pDialog = new ProgressDialog(GridViewActivity.this); 
       pDialog.setTitle("Connect to Server"); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 
      @Override 
      protected String doInBackground(Integer... args) { 
       // updating UI from Background Thread 
         Intent in = getIntent(); 
         songsList = new ArrayList<HashMap<String, String>>(); 
         cat_url = in.getStringExtra(KEY_CAT_URL); 
         artist_url = in.getStringExtra(KEY_CAT_ARTIST); 


         XMLParser parser = new XMLParser(); 
         String xml = parser.getXmlFromUrl(URL); // getting XML from URL 
         Document doc = parser.getDomElement(xml); // getting DOM element 

         NodeList nl = doc.getElementsByTagName(KEY_SONG); 
         // looping through all song nodes <song> 
         int page = args[0]; 
       int limit = 20; // items per page 
       int istart = page * limit; // what item to start on (onCreate() calls it with 0 and 0 * 20 = 0) 
       int iend = istart + limit; // how far to go 
       for (int i = istart; (i < nl.getLength()) && (i < iend); i++) { 
          // creating new HashMap 
          HashMap<String, String> map = new HashMap<String, String>(); 
          Element e = (Element) nl.item(i); 
          // adding each child node to HashMap key => value 
          map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
          map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL)); 
          map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL)); 
          // adding HashList to ArrayList 
          songsList.add(map); 

         } 
         return null; 
        } 

      @Override 
      protected void onPostExecute(String args) { 
       adapter=new GridViewActivityLazyAdapter(GridViewActivity.this, songsList); 
       grid.setAdapter(adapter); 
       pDialog.dismiss(); 
       } 
      } 
+1

你所說的「正常工作」的意思? – pawelzieba

回答

0

改變這一行

for (int i = 0; i < nl.getLength(); i++) { 

int page = args[0]; 
int limit = 20; // items per page 
int istart = page * limit; // what item to start on (onCreate() calls it with 0 and 0 * 20 = 0) 
int iend = istart + limit; // how far to go 
for (int i = istart; (i < nl.getLength()) && (i < iend); i++) { 

的onCreate()變化

new loadGridView().execute(); 

new loadGridView().execute(0); 

,或者會在頁面*限行一個空指針異常;

+0

謝謝羅伯特,你能告訴我我的onscolllistener上應該做些什麼來使它工作嗎?我的gridview現在只顯示20項。你能解決我的onscrolllistener的邏輯嗎? –

+0

嘗試使用無盡的適配器https://github.com/commonsguy/cwac-endless我相信它做同樣的事情,你想完成。 –

0

您發送到loadGridView「currentPage + 20」,然後乘以限制,這是20,所以它是一個巨大的數字。而你說

for (int i = istart; (i < nl.getLength()) && (i < iend); i++) 
// for (int i = hugeNumber; (i<probablyLessNumber) && (i < iend); i++) 

因此,它可能甚至沒有進入本聲明 我認爲你應該是這樣的:

if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 
      // I load the next page of gigs using a background task, 
      // but you can call any function here. 
      new loadGridView().execute(currentPage + 1); 
      loading = true; 
     }