我的自定义ListView
包含ImageView
,TextView
和CheckBox
。
这个问题对于一些程序员来说可能看起来很古老,但我真的找不到一个明确的解决方案。我的问题在于ImageView
。当我滚动图像更改时。
一些细节是:
RecyclerView
CheckBox
的状态,我正在使用getTag
和setTag
方法。他们工作正常。AsyncTask
我想加载图片和视频的缩略图,加载后会将它们附加到相应的ImageView
。 ---->这就是问题所在。我无法保持ImageView
的位置。即使在滚动非图像ImageView
(即没有与图像或视频相关的图像)也会丢失其默认图像。
以下是我在getView
类中实施的ListAdapter
代码:
@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 = 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());
viewHolder.iv.setImageResource(img);
Log.v("this","viewHolderMT: "+String.valueOf(viewHolder)+" position: "+position);
final View finalConvertView = convertView;
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);
//iv.setImageBitmap(null);
}
@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","viewHolderDIB: "+String.valueOf(viewHolder)+"position: "+position);
//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","viewHolderDIB: "+String.valueOf(viewHolder)+"position: "+position);
// publishProgress(thumb);
}
return thumb;
}
@Override
protected void onProgressUpdate(Bitmap... thumb){
}
@Override
protected void onPostExecute(Bitmap thumb){
/*if(isCancelled()){
return;
}
*/
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")) {
//Log.v("this","filenameOPE: "+String.valueOf(filenames[pos])+" positionOPE: "+String.valueOf(pos));
iv.setImageBitmap(thumb);
//iv.setTag(pos);
// Log.v("this", "In PU: " + String.valueOf(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")) {
// iv.setImageBitmap(thumb);
//}
//Log.v("this ", "Tag: " + String.valueOf(pos));
}
}
}
此处文件名是一个文件array
,其中包含将主要活动传递到列表adapter
类的文件列表。
P.S。如果您愿意,我也可以使用主要活动更新帖子。提前致谢。我是新手所以请合作。
答案 0 :(得分:1)
尝试在适配器中添加这两个方法并检查
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}