2017-05-27 180 views
-1

我想製作一個文件瀏覽器。除了我的圖像縮略圖加載速度太慢之外,我實現了這一切。所以我讀到我必須轉向asynctask,然後在後期執行更新ui。在運行asynctask之前,我爲我的所有文件提供了一個默認縮略圖。在asynctask中,我將位置作爲參數,因爲我使用它們來設置標籤。然後我使用那個位置檢查文件。如果這是圖像或視頻,我爲它創建一個位圖。在post執行後,我創建一個新的viewholder並使用getTag(position)爲其分配相同的標記。然後我使用setBitmapImage分配位圖。問題:即使對於不是圖像/視頻的文件,縮略圖也在改變/閃爍。 事情到目前爲止,我曾嘗試: 一)updating of Image Thumbnails using AsyncTask for Android ListView not coming proper
B)Android ListView updating of Image Thumbnails using AsyncTask Causes View recycling圖像縮略圖設置不正確

這裏是我的getView代碼:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 


    File file=new File(String.valueOf(filenames[position])); 
    if(file.isDirectory()){ 
     img=R.drawable.folder; 
    } 

    else{ 
     img=getImage(filenames[position]); 
    } 

    ViewHolder viewHolder = null; 
    if (convertView == null) { 
     LayoutInflater inflator = LayoutInflater.from(getContext()); 
     convertView = inflator.inflate(R.layout.list_item, null); 
     viewHolder = new ViewHolder(); 
     viewHolder.tv = (TextView) convertView.findViewById(R.id.textView1); 
     viewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1); 
     viewHolder.iv= (ImageView)convertView.findViewById(R.id.imageView); 
     viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
       myList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 
      } 
     }); 
     convertView.setTag(viewHolder); 
     convertView.setTag(R.id.textView1, viewHolder.tv); 
     convertView.setTag(R.id.checkBox1, viewHolder.cb); 
     convertView.setTag(R.id.checkBox1, viewHolder.iv); 
    } 
    else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
    viewHolder.iv.getLayoutParams().height = 80; 
    viewHolder.iv.getLayoutParams().width = 80; 
    viewHolder.cb.setTag(position); 
    final ViewHolder ViewHolder= viewHolder; 
    ViewHolder.iv.setImageResource(img); 
    ViewHolder.tv.setText(myList.get(position).getName()); 
    ViewHolder.cb.setChecked(myList.get(position).isSelected()); 


    final View finalConvertView = convertView; 
    class myTask extends AsyncTask<Integer,Void,Bitmap>{ 

     int pos; 
     public myTask(int pos){ 
      this.pos=pos; 
     } 

     @Override 
     protected Bitmap doInBackground(Integer... params) { 
      Bitmap thumb=null; 

      if((String.valueOf(filenames[pos])).contains(".jpeg")||String.valueOf(filenames[pos]).contains(".jpg")||String.valueOf(filenames[pos]).contains(".png")||String.valueOf(filenames[pos]).contains(".bmp")||String.valueOf(filenames[pos]).contains(".webp")||String.valueOf(filenames[pos]).contains(".gif")){ 
       thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(String.valueOf(filenames[pos])), 64, 64); 

      } 
      else if((String.valueOf(filenames[pos])).contains(".mp4")||String.valueOf(filenames[pos]).contains(".mkv")||String.valueOf(filenames[pos]).contains(".webm")||String.valueOf(filenames[pos]).contains(".3gp")||String.valueOf(filenames[pos]).contains(".3gpp")){ 
       thumb=ThumbnailUtils.createVideoThumbnail(filenames[pos].getPath(), MediaStore.Video.Thumbnails.MINI_KIND); 

      } 
      return thumb; 
     } 
     @Override 
     protected void onPostExecute(Bitmap thumb){ 
      if(isCancelled()){ 
       return; 
      } 
      ViewHolder v= (ViewHolder) finalConvertView.getTag(pos); 

      if((String.valueOf(filenames[pos])).contains(".jpeg")||String.valueOf(filenames[pos]).contains(".jpg")||String.valueOf(filenames[pos]).contains(".png")||String.valueOf(filenames[pos]).contains(".bmp")||String.valueOf(filenames[pos]).contains(".webp")||String.valueOf(filenames[pos]).contains(".gif")){ 

       v.iv.setImageBitmap(thumb); 
      } 
      else if((String.valueOf(filenames[pos])).contains(".mp4")||String.valueOf(filenames[pos]).contains(".mkv")||String.valueOf(filenames[pos]).contains(".webm")||String.valueOf(filenames[pos]).contains(".3gp")||String.valueOf(filenames[pos]).contains(".3gpp")){ 

       v.iv.setImageBitmap(thumb); 
      } 


     } 
    } 

    myTask m=new myTask(position); 
    m.execute(); 

    return convertView; 



} 

回答

0

我終於找到了答案。

這裏是ListAdapter類別:

class ListAdapter extends ArrayAdapter<Modal> { 

    private final Context context; 
    private int img; 

    private List<Modal> myList; 
    private File filenames[]; 
    private List<Integer> selectedItems=new ArrayList<Integer>(); 
    private CheckBox cb; 
    public ListAdapter(Context context, List<Modal> list, File filenames[]) { 
     super(context, R.layout.list_item, list); 
     this.context = context; 
     this.myList = list; 
     this.filenames=filenames; 
    } 


    @Override 
    public int getViewTypeCount() { 

     if(getCount()>0){ 
      return getCount(); 
     } 
     return 1; 
    } 

    @Override 
    public int getItemViewType(int position) { 

     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


    static class ViewHolder { 
     TextView tv; 
     CheckBox cb; 
     static ImageView iv; 
    } 

    public int getImage(File filename){ 
     if(String.valueOf(filename).contains(".rar")||String.valueOf(filename).contains(".zip")){ 
      return R.drawable.archive; 
     } 
     else if(String.valueOf(filename).contains(".doc")||String.valueOf(filename).contains(".docx")){ 
      return R.drawable.doc; 
     } 
     else if(String.valueOf(filename).contains(".pdf")){ 
      return R.drawable.pdf; 
     } 
     else if(String.valueOf(filename).contains(".txt")){ 
      return R.drawable.txt; 
     } 
     else if(String.valueOf(filename).contains(".ppt")||String.valueOf(filename).contains(".pptx")){ 
      return R.drawable.ppt; 
     } 
     else if(String.valueOf(filename).contains(".xls")||String.valueOf(filename).contains(".xlsx")){ 
      return R.drawable.xls; 
     } 
     else if((String.valueOf(filename).contains(".jpeg")||String.valueOf(filename).contains(".jpg")||String.valueOf(filename).contains(".png")||String.valueOf(filename).contains(".bmp")||String.valueOf(filename).contains(".webp")||String.valueOf(filename).contains(".gif"))){ 
      return R.drawable.image; 
     } 
     else if((String.valueOf(filename).contains(".mp4")||String.valueOf(filename).contains(".mkv")||String.valueOf(filename).contains(".webm")||String.valueOf(filename).contains(".3gp")||String.valueOf(filename).contains(".3gpp"))||String.valueOf(filename).contains(".MOV")){ 
      return R.drawable.video; 
     } 
     else if((String.valueOf(filename).contains(".mp3")||String.valueOf(filename).contains(".wav")||String.valueOf(filename).contains(".wma")||String.valueOf(filename).contains(".MP2")||String.valueOf(filename).contains(".AAC")||String.valueOf(filename).contains(".AC3")||String.valueOf(filename).contains(".AU")||String.valueOf(filename).contains(".OGG")||String.valueOf(filename).contains(".FLAC"))){ 
      return R.drawable.music; 
     } 

     else { 
      return R.drawable.unknown; 
     } 

    } 


    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 




     File file=new File(String.valueOf(filenames[position])); 
     if(file.isDirectory()){ 
      img=R.drawable.folder; 
     } 

     else{ 
      img=getImage(filenames[position]); 
     } 


     ViewHolder viewHolder = null; 

     if (convertView == null) { 
      LayoutInflater inflator = LayoutInflater.from(getContext()); 
      convertView = inflator.inflate(R.layout.list_item, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.tv = (TextView) convertView.findViewById(R.id.textView1); 
      viewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1); 
      viewHolder.iv= (ImageView)convertView.findViewById(imageView); 
      viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
        myList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 
       } 
      }); 
      convertView.setTag(viewHolder); 
      convertView.setTag(R.id.textView1, viewHolder.tv); 
      convertView.setTag(R.id.checkBox1, viewHolder.cb); 
      convertView.setTag(R.id.checkBox1, viewHolder.iv); 
     } 
     else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     viewHolder.iv.getLayoutParams().height = 150; 
     viewHolder.iv.getLayoutParams().width = 150; 
     viewHolder.cb.setTag(position); 
     final ViewHolder ViewHolder= viewHolder; 

     ViewHolder.tv.setText(myList.get(position).getName()); 
     ViewHolder.cb.setChecked(myList.get(position).isSelected()); 
     viewHolder.iv.setImageResource(img); 


     Log.v("this","viewHolderMT: "+String.valueOf(viewHolder)+" position: "+position); 
     myTask m=new myTask(position,viewHolder.iv); 
     m.execute(); 

     return convertView; 



    } 
    class myTask extends AsyncTask<Integer,Bitmap,Bitmap>{ 



     int pos; 
     ImageView iv; 
     public myTask(int pos,ImageView iv){ 
      this.pos=pos; 
      this.iv=iv; 
      iv.setTag(pos); 


     } 

     @Override 
     protected Bitmap doInBackground(Integer... params) { 
      Bitmap thumb=null; 


      if((String.valueOf(filenames[pos])).contains(".jpeg")||String.valueOf(filenames[pos]).contains(".jpg")||String.valueOf(filenames[pos]).contains(".png")||String.valueOf(filenames[pos]).contains(".bmp")||String.valueOf(filenames[pos]).contains(".webp")||String.valueOf(filenames[pos]).contains(".gif")){ 

       thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(String.valueOf(filenames[pos])), 150, 150); 

      } 
      else if((String.valueOf(filenames[pos])).contains(".mp4")||String.valueOf(filenames[pos]).contains(".mkv")||String.valueOf(filenames[pos]).contains(".webm")||String.valueOf(filenames[pos]).contains(".3gp")||String.valueOf(filenames[pos]).contains(".3gpp")||String.valueOf(filenames[pos]).contains(".MOV")){ 
       thumb=ThumbnailUtils.createVideoThumbnail(filenames[pos].getPath(), MediaStore.Video.Thumbnails.MINI_KIND); 


      } 
      return thumb; 
     } 
     @Override 
     protected void onProgressUpdate(Bitmap... thumb){ 


     } 
     @Override 
     protected void onPostExecute(Bitmap thumb){ 


      if(thumb!=null && (Integer)iv.getTag()==this.pos) { 
       if ((String.valueOf(filenames[pos])).contains(".jpeg") || String.valueOf(filenames[pos]).contains(".jpg") || String.valueOf(filenames[pos]).contains(".png") || String.valueOf(filenames[pos]).contains(".bmp") || String.valueOf(filenames[pos]).contains(".webp") || String.valueOf(filenames[pos]).contains(".gif")) { 

        iv.setImageBitmap(thumb); 

       } 
       else if ((String.valueOf(filenames[pos])).contains(".mp4") ||String.valueOf(filenames[pos]).contains(".MOV")|| String.valueOf(filenames[pos]).contains(".mkv") || String.valueOf(filenames[pos]).contains(".webm") || String.valueOf(filenames[pos]).contains(".3gp") || String.valueOf(filenames[pos]).contains(".3gpp")) { 

        iv.setImageBitmap(thumb); 
       } 


      } 





     } 
    } 
} 

錯誤我正在:

  • 移動的AsyncTask外getview()
  • 給異步構造內側向ImageView的一個參考
  • 還要實現getItemViewType()和getItemId()函數
  • 不是c在onPostExecute中檢查位置標記,以便圖像被洗牌。

這花了一段時間讓我弄清楚。希望它可以幫助別人!

+0

任何有關效率或其他方面的建議都非常受歡迎。 –