2012-08-15 17 views
0

我試圖創建一個無盡的圖像列表,每當您滾動到底部時加載5個項目。首先,我看了Mark Mooibroeks的代碼。它基本上是一個日期列表。但是我希望在每次向下滾動而不是文本時加載圖像的效果(每次更新時都會將其鏈接從MySQL數據庫中取出)。無限列表中的圖像而不是字符串

package com.pxr.tutorials.neverendinglist; 

import java.util.ArrayList; 
import java.util.Calendar; 

import android.app.ListActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.AbsListView; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.ArrayAdapter; 


public class NeverEndingList extends ListActivity { 

    //how many to load on reaching the bottom 
    int itemsPerPage = 5; 
    boolean loadingMore = false; 




    ArrayList<String> myListItems; 
    ArrayAdapter<String> adapter; 

    //For test data :-) 
    Calendar d = Calendar.getInstance(); 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState); 

     //Remove title bar 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     //Remove notification bar 
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.listplaceholder); 


     //This will hold the new items 
     myListItems = new ArrayList<String>(); 
     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myListItems); 




     //add the footer before adding the adapter, else the footer will nod load! 
     View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false); 
     this.getListView().addFooterView(footerView); 

     //Set the adapter 
     this.setListAdapter(adapter); 


     //Here is where the magic happens 
     this.getListView().setOnScrollListener(new OnScrollListener(){ 

      //useless here, skip! 
      @Override 
      public void onScrollStateChanged(AbsListView view, int scrollState) {} 

      //dumdumdum   
      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, 
        int visibleItemCount, int totalItemCount) { 


       //what is the bottom iten that is visible 
       int lastInScreen = firstVisibleItem + visibleItemCount;    

       //is the bottom item visible & not loading more already ? Load more ! 
       if((lastInScreen == totalItemCount) && !(loadingMore)){     
        Thread thread = new Thread(null, loadMoreListItems); 
        thread.start(); 
       } 
      } 
     }); 


     //Load the first 15 items 
     Thread thread = new Thread(null, loadMoreListItems); 
     thread.start(); 

    } 


    //Runnable to load the items 
    private Runnable loadMoreListItems = new Runnable() {   
     @Override 
     public void run() { 
      //Set flag so we cant load new items 2 at the same time 
      loadingMore = true; 

      //Reset the array that holds the new items 
      myListItems = new ArrayList<String>(); 

      //Simulate a delay, delete this on a production environment! 
      try { Thread.sleep(1000); 
      } catch (InterruptedException e) {} 

      //Get 15 new listitems 
      for (int i = 0; i < itemsPerPage; i++) {   

       //Fill the item with some bogus information 
       myListItems.add("Date: " + (d.get(Calendar.MONTH)+ 1) + "/" + d.get(Calendar.DATE) + "/" + d.get(Calendar.YEAR) + "http://www.test.com/" + i);    

       // +1 day :-D 
       d.add(Calendar.DATE, 1); 

      } 

      //Done! now continue on the UI thread 
      runOnUiThread(returnRes); 

     } 
    }; 


    //Since we cant update our UI from a thread this Runnable takes care of that! 
    private Runnable returnRes = new Runnable() { 
     @Override 
     public void run() { 

      //Loop thru the new items and add them to the adapter 
      if(myListItems != null && myListItems.size() > 0){ 
       for(int i=0;i<myListItems.size();i++) 
        adapter.add(myListItems.get(i)); 
      } 

      //Update the Application title 
      setTitle("Neverending List with " + String.valueOf(adapter.getCount()) + " items"); 

      //Tell to the adapter that changes have been made, this will cause the list to refresh 
      adapter.notifyDataSetChanged(); 

      //Done loading more. 
      loadingMore = false; 
     } 
    }; 
} 

回答

0

而不是ArrayAdapter<String> adapter您必須實現您自己的自定義適配器。 搜索android custom adapter,你會發現有關它的信息/教程的負載。

+0

對我來說,這仍然是相當困難的。我想我會繼續嘗試,並回來與其他問題。如果你知道關於這個圖像列表的好教程,你可以隨時告訴我。 – s0169720 2012-08-19 17:27:29

相關問題