0

Meine GridView besteht aus Ansichten, die eine Download-Schaltfläche haben. Die Schaltfläche wird ausgeblendet, wenn Sie darauf tippen und eine Fortschrittsleiste anzeigt. Der Fortschrittsbalken wird von einem Dienst aktualisiert, der gestartet wird, wenn die Download-Schaltfläche gedrückt wird.Warum scheint GridView verschiedene Zellen zu verwechseln?

Das Problem, das ich habe, ist, wenn ich einen Download für ein bestimmtes Element starten und dann nach unten scrollen merke ich, dass der Fortschrittsbalken in Zellen sichtbar ist, die nicht heruntergeladen werden sollen. Ich denke, das wird wahrscheinlich durch GridView-Recycling-Zellen verursacht, aber ich bin mir nicht ganz sicher. Ich habe etwas von dem Code unten gepostet. Ich kann mehr bei Bedarf posten.

getView - in Adapter für Grid

public View getView(final int position, View convertView, ViewGroup parent) { 
      final RelativeLayout card; 
      if (convertView == null) { 
       card = (RelativeLayout) getLayoutInflater().inflate(R.layout.book_cover, null); 
      } else { 
       card = (RelativeLayout) convertView; 
      } 
      TextView bookName = (TextView) card.findViewById(R.id.textView_bookName); 
      TextView authorName = (TextView) card.findViewById(R.id.textView_bookAuthor); 
      final MNBook currentBook = getItem(position); 
       bookName.setText((String) currentBook.getTitle()); 
      authorName.setText((String) currentBook.getAuthor()); 
      final Button downloadBookButton = (Button) card.findViewById(R.id.button_download_book); 
      final Button readBookButton = (Button) card.findViewById(R.id.button_read_book); 
      final ProgressBar progressBar = (ProgressBar) card.findViewById(R.id.progressbar_book_download); 
      readBookButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent(); 
        intent.setAction(Intent.ACTION_VIEW); 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { 
         intent.setDataAndType(Uri.fromFile(new File(getExternalFilesDir(null), 
             currentBook.getFileName())), 
           "application/pdf"); 
        } 
        startActivity(intent); 
       } 
      }); 
      downloadBookButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        downloadFile(currentBook, progressBar, 
          downloadBookButton, readBookButton); 
       } 
      }); 
      if (currentBook.isBooleanDownloaded()) { 
       downloadBookButton.setVisibility(View.GONE); 
       readBookButton.setVisibility(View.VISIBLE); 
       progressBar.setVisibility(View.GONE); 
      } 

//    ImageView bgImage = (ImageView) card.findViewById(R.id.myImageView); 
      Resources resources = getApplicationContext().getResources(); 
//    final int resourceId = resources.getIdentifier(mCategoryImages[position], "drawable", 
//      getApplicationContext().getPackageName()); 
//    bgImage.setImageResource(resourceId); 
      return card; 
     } 

Downloadfile Methode

private void downloadFile(MNBook book, final ProgressBar progressBar, 
           final Button downloadButton, final Button readButton) { 
     final MNBook localBook = book; 
     String fileName = book.getFileName(); 
     String url = book.getURL(); 
     Intent intent = new Intent(this, DownloadService.class); 
     intent.putExtra("url", url); 
     intent.putExtra("file_name", fileName); 
     intent.putExtra("receiver", new ResultReceiver(new Handler()) { 
      @Override 
      protected void onReceiveResult(int resultCode, Bundle resultData) { 
       super.onReceiveResult(resultCode, resultData); 
       if (resultCode == DownloadService.UPDATE_PROGRESS) { 
        int progress = resultData.getInt("progress"); 
//     mDetailFragment.setProgress(progress); 
//       mProgressDialog.setProgress(progress); 
        progressBar.setProgress(progress); 
        if (progress == 100) { 
         progressBar.setVisibility(View.GONE); 
         readButton.setVisibility(View.VISIBLE); 
         setDownloadedBookStatus(localBook); 
        } 
       } 
      } 
     }); 
     downloadButton.setVisibility(View.GONE); 
     progressBar.setVisibility(View.VISIBLE); 
     startService(intent); 

    } 

Antwort

0

benötigen Sie Widget Sichtbarkeit aktualisieren auf jedem getView() Anruf, nicht nur einige von ihnen. Dies ist nicht anders als die Notwendigkeit, den Text oder das Bild bei jedem Aufruf auf getView() festzulegen. Widgets in AdapterView und RecyclerView Klassen, wie GridView, werden recycelt. Wenn Sie die ProgressBar in einem getView() Aufruf anzeigen, und das Widget wird recycelt und Sie nicht speziell für den recycelten getView() Anruf verstecken, ist die ProgressBar immer noch sichtbar.