我想製作一個文件瀏覽器。除了我的圖像縮略圖加載速度太慢之外,我實現了這一切。所以我讀到我必須轉向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;
}
任何有關效率或其他方面的建議都非常受歡迎。 –