2012-02-16 24 views
0

我需要防止在我的定製列表視圖中的延遲加載。所以我發現scrollview onscroll listner在那裏有幫助,我已經嘗試過但沒有成功實現這個功能。在scrolllistview加載記錄

代碼:

lviewAdapter = new ListViewAdapter(SaxParser2.this, Arr_ActivityName, Arr_AudioScript,Arr_RequestID,Arr_FolderPath,Arr_RequestTo); 
    lview.setOnScrollListener(SaxParser2.this);  

@Override 
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) { 
    // TODO Auto-generated method stub 
    lview.setAdapter(lviewAdapter); 
} 
@Override 
public void onScrollStateChanged(AbsListView arg0, int arg1) { 
    // TODO Auto-generated method stub  
} 

這裏是我的BaseAdapter

package com.RecordingApp; 

import it.sauronsoftware.ftp4j.FTPClient; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.io.StringWriter; 
import java.io.Writer; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.graphics.Typeface; 
import android.graphics.drawable.AnimationDrawable; 
import android.media.MediaPlayer; 
import android.media.MediaRecorder; 
import android.os.AsyncTask; 
import android.os.CountDownTimer; 
import android.os.Environment; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.Sharedpreferance.GMailSender; 

public class ListViewAdapter extends BaseAdapter { 
    public static Activity context; 

    String title[]; 
    String description[]; 
    String RequestId[]; 
    String Folderpath[]; 
    String RequestTo[]; 

    boolean b_Flag_Record_or_not = false; 
    boolean b_upload_or_not = false; 
    boolean start_or_not = false; 
    boolean start_play = false; 
    boolean upload_or_not = false; 
    boolean b_play_or_not = false; 
    boolean isplaying2 = false; 

    Thread welcomeThread,welcomeThread2; 
    int glob_position; 
    MediaPlayer mPlayer2; 
    int flag_stop_position; 
    AudioRecorder recorder; 
    AnimationDrawable frameAnimation, frameAnimation_play; 
    private static String mFileName = null; 
    private MediaRecorder mRecorder = null; 
    private MediaPlayer mPlayer = null; 
    Recording login = new Recording(); 


    MediaPlayer MP_completeRequest = new MediaPlayer(); 

    public ListViewAdapter(Activity context, String[] title, 
      String[] description, String[] req_id, String[] FolderPath, 
      String[] Arr_RequestTo) { 
     super(); 
     this.context = context; 
     this.title = title; 
     this.description = description; 
     this.RequestId = req_id; 
     this.Folderpath = FolderPath; 
     this.RequestTo = Arr_RequestTo; 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return title.length; 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    private class ViewHolder { 
     TextView txtViewTitle; 
     TextView txtViewDescription; 
     Button record, stop, play, upload; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final ViewHolder holder; 
     LayoutInflater inflater = context.getLayoutInflater(); 
     glob_position = position; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.listitem_row, null); 
      holder = new ViewHolder(); 
      holder.txtViewTitle = (TextView) convertView 
        .findViewById(R.id.textView1); 
      holder.txtViewDescription = (TextView) convertView 
        .findViewById(R.id.textView2); 
      holder.record = (Button) convertView.findViewById(R.id.record); 

      holder.stop = (Button) convertView.findViewById(R.id.stop); 
      holder.play = (Button) convertView.findViewById(R.id.play1); 
      holder.upload = (Button) convertView 
        .findViewById(R.id.audio_upload); 

      holder.record.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 

        if (isplaying2 == true) { 

        } else { 
         if (b_Flag_Record_or_not == true) { 
         } else { 
          try { 

           holder.record.setBackgroundResource(R.drawable.record_green); 
           b_Flag_Record_or_not = true; 
           b_play_or_not = true; 

           flag_stop_position = position; 
           AuthHandler dataHandler = new AuthHandler(); 
           AuthDataset dataset = dataHandler 
             .getParsednewJobdtl_DataSet(); 


           // Login l = new Login(); 
           String str_useid = RequestTo[position]; 
           recorder = new AudioRecorder(
             "/audiometer/shanesh" 
               + RequestId[position] + "-" 
               + str_useid); 
           start_or_not = true; 
           recorder.start(); 
           Toast.makeText(context, "Recording Started", 
             Toast.LENGTH_LONG).show(); 
           CountDownTimer countDowntimer = new CountDownTimer(
             120000000, 1000) { 
            public void onTick(long millisUntilFinished) { 
            } 

            public void onFinish() { 
             try { 
              Toast.makeText(
                context, 
                "Stop recording Automatically ", 
                Toast.LENGTH_LONG).show(); 
              recorder.stop(); 
             } catch (Exception e) { 
              // TODO Auto-generated catch block 
              e.printStackTrace(); 
             } 
            } 
           }; 
           countDowntimer.start(); 

          } catch (IOException e) { 
           gotoGmail(e); 
          } catch (Exception e) { 
           gotoGmail(e); 
          } 
         } 

        } 
       } 
      }); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     try { 

      Typeface face = Typeface.createFromAsset(context.getAssets(), 
        "fonts/tahoma.ttf"); 
      holder.txtViewTitle.setTypeface(face); 

      holder.txtViewTitle.setText(title[position]); 
      holder.txtViewDescription.setText(description[position]); 

     } catch (ArrayIndexOutOfBoundsException e) { 
      e.printStackTrace(); 
     } 

     holder.stop.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       try { 
        if (isplaying2 == true) { 
         mPlayer2.stop(); 

         isplaying2 = false; 
        } else { 
        holder.record.setBackgroundResource(R.drawable.page7_15); 
         if (flag_stop_position == position) { 
          b_Flag_Record_or_not = false; 
          if (start_or_not == true) { 
           b_play_or_not = false; 

           recorder.stop(); 
           upload_or_not = true; 
           start_play = true; 
           Toast.makeText(context, "Stoped Recording", 
             Toast.LENGTH_LONG).show(); 
          } else { 
           Toast.makeText(context, 
             " Please Start recording ", 
             Toast.LENGTH_LONG).show(); 

          } 
         } else { 

         } 

        } 

       } catch (Exception e) { 
        gotoGmail(e); 
       } 
      } 
     }); 
     holder.play.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       String temp = SaxParser2.str_completeORpendingFlag; 

       if (temp.equalsIgnoreCase("c")) { 
        try { 

         final MediaPlayer mPlayer = new MediaPlayer(); 
         try { 

          mPlayer.setDataSource(Folderpath[position]); 

          mPlayer.prepare(); 

          final int welcomeScreenDisplay = mPlayer 
            .getDuration(); 
          welcomeThread = new Thread() { 
           int wait = 0; 

           @Override 
           public void run() { 
            try { 
             isplaying2 = true; 
             mPlayer.start(); 
             while (wait < welcomeScreenDisplay) { 
              sleep(100); 
              wait += 100; 
             } 
            } catch (Exception e) { 

            } finally { 
             isplaying2 = false; 
            } 
           } 
          }; 
          welcomeThread.start(); 

         } catch (Exception e) { 
          gotoGmail(e); 
         } 

        } catch (Exception e) { 
         gotoGmail(e); 
        } 
       } else if (temp.equalsIgnoreCase("p")) { 
        try { 

         if (b_play_or_not == true) { 

         } else { 

          String str_useid = RequestTo[position]; 
          java.io.File file = new java.io.File(Environment 
            .getExternalStorageDirectory() 
            + "/audiometer/", "shanesh" 
            + RequestId[position] + "-" + str_useid 
            + ".amr"); 
          if (file.exists()) { 

           Toast.makeText(context, "Playing", 
             Toast.LENGTH_LONG).show(); 

           mPlayer2 = new MediaPlayer(); 
           try { 
            mPlayer2.setDataSource(Environment 
              .getExternalStorageDirectory() 
              + "/audiometer/shanesh" 
              + RequestId[position] 
              + "-" 
              + str_useid + ".amr"); 
            mPlayer2.prepare(); 
            mPlayer2.start(); 
            isplaying2 = true; 

            final int welcomeScreenDisplay = mPlayer2 
              .getDuration(); 
            /** 
            * create a thread to show splash up to 
            * splash time 
            */ 
            welcomeThread = new Thread() { 
             int wait = 0; 

             @Override 
             public void run() { 
              try {mPlayer2.prepare(); 

               while (wait < welcomeScreenDisplay) { 
                sleep(100); 
                wait += 100; 
               } 
              } catch (Exception e) { 

              } finally { 
               welcomeThread2 = new Thread() { 
                int wait = 0; 

                @Override 
                public void run() { 
                 try { 
                  mPlayer2.start(); 
                  while (wait < welcomeScreenDisplay) { 
                   sleep(100); 
                   wait += 100; 
                  } 
                 } catch (Exception e) { 

                 } finally { 
                  // frameAnimation_play.stop(); 
                  isplaying2 = false; 
                 } 
                } 
               }; 
               welcomeThread2.start(); 

              } 
             } 
            }; 
            welcomeThread.start(); 


           } catch (Exception e) { 
            gotoGmail(e); 
           } 
          } else { 
           Toast.makeText(context, " File Not Found ", 
             Toast.LENGTH_LONG).show(); 

          } 
         } 

        } catch (Exception e) { 

         gotoGmail(e); 
        } 
       } 

      } 
     }); 

     holder.upload.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       glob_position = position; 
       // String str_useid = RequestTo[position]; 
       if (upload_or_not == true) { 
        try { 
         new xyz().execute(); 

        } catch (Exception e) { 
         gotoGmail(e); 
        } 
       } 

      } 

     }); 
     return convertView; 
    } 

    private class xyz extends AsyncTask<Void, Void, Void> { 
     private final ProgressDialog dialog = new ProgressDialog(context); 

     protected void onPreExecute() { 
      try { 
       this.dialog.setMessage("Uploading...Please Wait.."); 
       dialog.setIndeterminate(true); 
       dialog.setCancelable(false); 
       this.dialog.show(); 

       // put your code which preload with processDialog 
      } catch (Exception e) { 
       gotoGmail(e); 
      } 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      for (int i = 0; i < 100000; i++) { 

      } 
      it.sauronsoftware.ftp4j.FTPClient con = new it.sauronsoftware.ftp4j.FTPClient(); 
      try { 
       String str_useid = RequestTo[glob_position]; 
       con.connect("URL"); 
       con.login("as", "asdasd"); 
       con.changeDirectory("/rangam/RequestContent/audio"); 
       con.setPassive(true); 
       con.setType(FTPClient.TYPE_BINARY); 

       con.upload(new java.io.File(Environment 
         .getExternalStorageDirectory() 
         + "/audiometer/shanesh" 
         + RequestId[glob_position] + "-" + str_useid + ".amr")); 

       String filename = "/shanesh" + RequestId[glob_position] + "-" 
         + str_useid + ".amr"; 
       con.logout(); 
       con.disconnect(true); 
       String MachineName = Machinelist.str_Machinename; 
       sendFlagToServer(RequestId[glob_position], filename, 
         MachineName); 

      } catch (Exception e) { 
       gotoGmail(e); 
      } 

      return null; 

     } 

     protected void onPostExecute(final Void unused) { 

      if (this.dialog.isShowing()) { 
       try { 
        this.dialog.dismiss(); 
        AlertDialog.Builder alertbox = new AlertDialog.Builder(
          context); 
        alertbox.setTitle("Message"); 
        alertbox.setMessage(":: Uploaded Successfully ::"); 
        alertbox.setPositiveButton("OK", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface arg0, 
             int arg1) { 
            Intent intent_request = new Intent(context, 
              SaxParser2.class); 
            intent_request.putExtra("value", Machinelist.str_UIDValue); 
            intent_request.putExtra("machineName", 
              Machinelist.str_Machinename); 
            intent_request.putExtra("captureBack_or_not", 2); 
            context.startActivity(intent_request); 
            context.finish(); 
           } 
          }); 
        alertbox.show(); 

       } catch (Exception e) { 
        gotoGmail(e); 
       } 
      } 

     } 
    } 

    private void sendFlagToServer(String requestID, String filename, 
      String machineName) { 
     HttpURLConnection connection; 
     OutputStreamWriter request = null; 
     String MachineName = Machinelist.str_Machinename; 
     URL url = null; 
     String parameters = "RequestID=" + requestID + "&FileName=" + filename 
       + "&MachineName=" + MachineName + "&RequestType=A"; 
     try { 
      url = new URL(
        "URL"); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoOutput(true); 
      connection.setRequestProperty("Content-Type", 
        "application/x-www-form-urlencoded"); 
      connection.setRequestMethod("POST"); 

      request = new OutputStreamWriter(connection.getOutputStream()); 
      request.write(parameters); 
      request.flush(); 
      request.close(); 
      String line = ""; 
      InputStreamReader isr = new InputStreamReader(
        connection.getInputStream()); 
      BufferedReader reader = new BufferedReader(isr); 
      StringBuilder sb = new StringBuilder(); 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 

      isr.close(); 
      reader.close(); 

     } catch (Exception e) { 
      gotoGmail(e); 
     } 

    } 

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK)) { 

      try { 

       context.finish(); 
      } catch (Exception e) { 
       gotoGmail(e); 
      } 

     } 
     return false; 
    } 
    void gotoGmail(Exception e) { 
     Writer writer = new StringWriter(); 
     PrintWriter printWriter = new PrintWriter(writer); 
     e.printStackTrace(printWriter); 
     String s = writer.toString(); 

    } 

} 

回答

1

好吧,

所以,首先你必須定義「什麼是視圖的數量我是否需要加載,以便我的用戶不會看到它是延遲加載?「 :

比方說,你要加載20行...

現在你onscroll,或任何你想要的,但像滾動或Adapter.getView的地方,你必須知道什麼是此刻顯示在屏幕上。讓我解釋一下:

我想加載20行,所以我的用戶不會注意到, 我是其中顯示10 ,其中10只是隱藏(不顯示,如果該傢伙不滾動)。

現在,我不想再加載10個,當這個傢伙剛剛打到我的第15排時 有什麼辦法可以做到這一點?

Humm,讓我們看看,試圖找到返回屏幕上顯示的最後一行的(ABSOLUTE)數字的東西 - 這可能是一個不錯的解決方案。 otehr解決方案可能是您的第10或第15行是第一個顯示在屏幕上?

等,等等......

我覺得這樣的東西應該做的伎倆。 ;)

0

這是我從網上抓取的樣本。它正在裝載ListViewlistening to the Scroll的項目。對於示例,它正在ListView中加載日期。到達結束位置後,它將加載下15個日期。它是Never Ending List。您可以將適配器修改爲您的自定義適配器。這個概念是:

  1. 您已設置數據
  2. 加載第一N無項目
  3. 設置滾動收聽到ListView
  4. 如果滾動ListView中到達最後一個可見的項目,你觸發工人線程加載額外的m個項目。
  5. 從UI線程通知列表視圖。

    int itemsPerPage = 15; 
    boolean loadingMore = false; 
    ArrayList<String> myListItems; 
    ArrayAdapter<String> adapter; 
    
    //For test data :-) 
    Calendar d = Calendar.getInstance(); 
    
    this.getListView().setOnScrollListener(new OnScrollListener(){ 
    
        //useless here, skip! 
        @Override 
        public void onScrollStateChanged(AbsListView view, int scrollState) {} 
    
        @Override 
        public void onScroll(AbsListView view, int firstVisibleItem, 
          int visibleItemCount, int totalItemCount) { 
    
    
         //what is the bottom item 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>(); 
    
    
        //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));    
    
         // +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; 
    } 
    }; 
    

編輯: 你可以得到的教程和完整的項目here

+0

我知道這件事情,但是怎麼樣在我的情況? – 2012-02-16 10:04:24

+0

你能爲我提供自定義list-view.onscrollview – 2012-02-16 10:05:30