1

我在listview項目中有按鈕,當它被點擊時,水平progressbar開始加載,當它完成時,加載兩個按鈕應該出現在listview中的那個項目中。當進度條下載時,如果我將該項目從屏幕上滾動下來,然後將其向後滾動,進度條消失並出現兩個按鈕。下載過程正常。但是我想在下載時一直顯示進度條? 在這裏我的適配器。請任何幫助。在此先感謝將視圖從屏幕上滾動後,列表視圖的視圖返回到原始狀態

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ArrayList<String> array=JournalArray.get(position); 
    LayoutInflater inflator = ((Activity) activity).getLayoutInflater(); 
    final Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>(); 
    final Button imgAR; 
    final Button imgPDF; 
    final Button imgDown; 
    final Button btnDel; 
    final TextProgressBar progress; 
    //ConnectionDetector cd=new ConnectionDetector(activity); 
    final Database db=new Database(activity); 
    //final ImageView imgViewFlag; 
    //final TextView txtViewTitle,txtProgress; 

    if(convertView==null) 
    { 
     convertView = inflator.inflate(R.layout.familylist_item, null); 
    }  
     progress=(TextProgressBar)convertView.findViewById(R.id.downprogress); 
     progress.setTextColor(Color.BLACK); 
     //progress.setTag(tag) 
     //progress.setVisibility(View.GONE); 
     TextView txtViewTitle = (TextView) convertView.findViewById(R.id.text); 
     //view.txtProgress=(TextView)convertView.findViewById(R.id.textProgress); 
     ImageView imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1); 
     imgAR=(Button)convertView.findViewById(R.id.imageAR); 
     //view.imgAR.setTag(view); 
     imgDown=(Button)convertView.findViewById(R.id.imageDown); 
     //view.imgDown.setTag(view); 
     imgPDF=(Button)convertView.findViewById(R.id.imagePDF); 
     //view.imgPDF.setTag(view); 
     //view.progress=(ProgressBar)convertView.findViewById(R.id.downprogress); 
     btnDel=(Button)convertView.findViewById(R.id.btnDel); 
     //view.btnDel.setTag(view); 
     //convertView.setTag(view); 

    if(DeleteBin){ 
     if(XMLParser.CheckFileInSD(array.get(5))){ 
     btnDel.setVisibility(View.VISIBLE); 
     imgAR.setVisibility(View.GONE); 
     imgDown.setVisibility(View.GONE); 
     imgPDF.setVisibility(View.GONE); 
     progress.setVisibility(View.GONE); 
     } 
     else{ 
      imgDown.setVisibility(View.GONE); 
      imgAR.setVisibility(View.GONE); 
      btnDel.setVisibility(View.GONE); 
      imgPDF.setVisibility(View.GONE); 
      progress.setVisibility(View.GONE); 
     } 
    } 
    else{ 
     btnDel.setVisibility(View.GONE); 
     if(!XMLParser.CheckFileInSD(array.get(5))){ 
      imgAR.setVisibility(View.GONE); 
      imgPDF.setVisibility(View.GONE); 
      imgDown.setVisibility(View.VISIBLE); 
      progress.setVisibility(View.GONE); 
     } 
     else{ 
      imgAR.setVisibility(View.VISIBLE); 
      imgPDF.setVisibility(View.VISIBLE); 
      imgDown.setVisibility(View.GONE); 
      progress.setVisibility(View.GONE); 
      } 
     } 

    txtViewTitle.setText(array.get(1)); 
    channelid=array.get(4); 
    RelativeLayout.LayoutParams paramrel = new RelativeLayout.LayoutParams(width/5, height/5); 
    paramrel.leftMargin = width/10; 
    imgViewFlag.setLayoutParams(paramrel); 
    imgViewFlag.setImageDrawable(Drawable.createFromPath(new File(array.get(3)).getAbsolutePath())); 
    btnDel.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      pos=position; 
      Deletefiles(pos); 
      Toast.makeText(activity, "Сэтгүүл амжилттай усгтгагдлаа", Toast.LENGTH_SHORT).show(); 
      btnDel.setVisibility(View.GONE); 
     } 
    }); 
    imgAR.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      channelid=array.get(4); 
      Intent intent=new Intent(activity,ARELViewActivity.class); 
      intent.putExtra("channelid", db.getConfigByChannelID(channelid)); 
      activity.startActivity(intent); 
     } 
    }); 
    imgPDF.setOnClickListener(new View.OnClickListener() { 

     @SuppressLint("SdCardPath") 
     @Override 
     public void onClick(View v) { 
      path=array.get(5); 
      String PATH=Environment.getExternalStorageDirectory().getAbsolutePath()+ "/sdcard/.FamilyMagazine/";  
      openBook(PATH+XMLParser.getFileName(path)); 
     } 
    }); 
    imgDown.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Database db=new Database(activity); 
      final ConnectionDetector cd=new ConnectionDetector(activity); 
      if(downloading<3){ 
      downloading++; 

      path=array.get(5); 
      channelid=array.get(4); 
      pos=position; 
      progress.setMax(100); 
      progress.setProgress(0); 
      progress.setVisibility(View.VISIBLE); 
      imgDown.setVisibility(View.GONE); 
      progress.setText("Татаж байна..."); 
      new AsyncTask<View, String, String>() { 
       private View v; 

       @Override 
       protected String doInBackground(View... params) { 
        v = params[0]; 
        int count; 
        isLoading=true; 
        //mprogress=progress; 
        String result="0"; 
        if(cd.isConnectingToInternet()){ 
         getChannel(channelid); 
         try { 
          URL url = new URL(path); 
          URLConnection conection = url.openConnection(); 
          conection.connect(); 
          // getting file length 
          lenghtOfFile = conection.getContentLength(); 

          // input stream to read file - with 8k buffer 
          InputStream input = new BufferedInputStream(url.openStream(), 8192); 

          String PATH="/mnt/sdcard/sdcard/.FamilyMagazine"; 
          File dir=new File(PATH); 
          boolean test=dir.mkdirs(); 
          OutputStream output = new FileOutputStream("/mnt/sdcard/sdcard/.FamilyMagazine/"+XMLParser.getFileName(path)); 
          byte data[] = new byte[1024]; 

          long total = 0; 

          while ((count = input.read(data)) != -1) { 
           total += count; 
           // publishing the progress.... 
           // After this onProgressUpdate will be called 
           publishProgress(""+(int)((total*100)/lenghtOfFile)); 

           // writing data to file 
           output.write(data, 0, count); 
           hash.put(pos, (int)((total*100)/lenghtOfFile)); 
          } 

          // flushing output 
          output.flush(); 

          // closing streams 
          output.close(); 
          input.close(); 


         } catch (Exception e) { 
          result=e.toString(); 
         } 
         } 
        else 
         result="Интернетэд холбогдоогүй байна!"; 

        return result; 
       } 
       protected void onProgressUpdate(String... progresses) { 
        // setting progress percentage 
       progress.setProgress(Integer.parseInt(progresses[0])); 
       int x=lenghtOfFile/100 * Integer.parseInt(progresses[0])/1048576; 
       progress.setText(x+"MB/"+lenghtOfFile/1048576+"MB"); 
       } 
       @Override 
       protected void onPostExecute(String result) { 
        super.onPostExecute(result); 
        //view.txtProgress.setVisibility(View.GONE); 
        downloading--; 

        isLoading=false; 
        if(result.equals("0")){ 
         imgDown.setVisibility(View.GONE); 
         progress.setVisibility(View.GONE); 
         btnDel.setVisibility(View.GONE); 
         imgAR.setVisibility(View.VISIBLE); 
         imgPDF.setVisibility(View.VISIBLE); 

        } 
        else 
        { 
         Toast.makeText(activity, result, Toast.LENGTH_LONG).show(); 

         imgDown.setVisibility(View.VISIBLE); 
         btnDel.setVisibility(View.GONE); 
         imgAR.setVisibility(View.GONE); 
         imgPDF.setVisibility(View.GONE); 


        } 

     } 
      }.execute(v); 
      } 
      else 
       Toast.makeText(activity, "Өөр сэтгүүл татаж байна\n Түр хүлээнэ үү", Toast.LENGTH_LONG).show(); 
     } 
    }); 

    return convertView; 

} 
+0

爲此目的使用UIL。緩存圖像。如果在滾動過程中已下載,則無需再次下載。您可以在下載 – Raghunandan 2013-05-14 11:01:50

+0

時顯示進度條,您可以在鏈接中檢查答案http://stackoverflow.com/questions/15985194/progressbar-in-each-gridview-item-android/15985261#15985261 – Raghunandan 2013-05-14 11:04:58

+0

您可以使用下載這是getview方法的一部分。當屏幕開始滾動時,這個方法被調用。 – 2013-05-14 11:15:28

回答

1

我遇到過這個問題。發生這種情況是因爲Listview重用視圖(行),它不會爲所有行創建視圖,它只會創建一些在屏幕上可見的行,其餘的行只是重用創建的視圖。

解決您的問題的方法是維護某些自定義對象(可以有這些對象的數組列表)中的每一行的狀態,然後在getView()方法中恢復該對象的所有視圖的狀態。 例如您可以將此對象中progressBar的狀態保存爲布爾值。只要有人按下按鈕,對象(對應於一個特定的行)應該更新爲'true'布爾值。每次在獲取視圖中,進度條的狀態都應該從這個對象中設置。

這應該可以解決您的問題。

+0

你能告訴我示例代碼嗎? – casper 2013-05-14 12:33:23

+1

很好的解決方案,謝謝 – 2014-04-11 12:13:44

相關問題