究竟如何使用asynctask?

时间:2017-05-27 20:25:14

标签: android android-asynctask lazy-loading

我花了好几天时间来实现文件浏览器。我只被困在一个部分。图像加载太慢!所以我转向asynctask。我无法正确更新imageview持有者。 这是我的代码:

@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.tv.setText(myList.get(position).getName());
    ViewHolder.cb.setChecked(myList.get(position).isSelected());

    Log.v("this","viewHolderMT: "+String.valueOf(viewHolder)+"position: "+position);

    final View finalConvertView = convertView;

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

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

        @Override
        protected Bitmap doInBackground(Integer... params) {
            Bitmap thumb=null;
            //Log.v("this","position: "+String.valueOf(position));

            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);
                publishProgress(thumb);
                Log.v("this","In DIB: "+String.valueOf(thumb));
                //Log.v("this","filenameDOI: "+String.valueOf(filenames[pos])+" positionDOI: "+String.valueOf(pos));
            }
            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);
                Log.v("this","In DIB: "+String.valueOf(thumb));
                publishProgress(thumb);

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

            ViewHolder v= (ViewHolder)finalConvertView.getTag();

            Log.v("this","viewHolderPU: "+String.valueOf(v)+"position: "+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")){
                //Log.v("this","filenameOPE: "+String.valueOf(filenames[pos])+" positionOPE: "+String.valueOf(pos));
                v.iv.setImageBitmap(thumb[0]);
                Log.v("this","In PU: "+String.valueOf(thumb[0]));
            }
            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[0]);
            }
        }
        @Override
        protected void onPostExecute(Bitmap thumb){



        }
    }

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

   return convertView;



}

图像加载到不同的图像视图,同时图像视图在滚动和其他方面的新图像上一直闪烁。我接近解决方案,但不知何故仍然卡住了。

1 个答案:

答案 0 :(得分:1)

您可以在onPostExecute()中获得AsyncTask的结果。 onProgressUpdate()用于显示用户界面上的进度条等内容。

尝试将代码从onProgressUpdate()移至onPostExecute()