gpt4 book ai didi

android - 滚动列表时 View 意外更改

转载 作者:行者123 更新时间:2023-11-29 17:53:59 25 4
gpt4 key购买 nike

我在扩充 ListView 时遇到了意想不到的问题。

我正在创建一个 ListView ,其中显示图像标题和缩略图。我们还可以显示音频文件缩略图,但只给出图像类。

当我快速滚动列表时。它的观点改变了很多次。在我的适配器类下面,我在其中使用 AsynkTask 类来获取缩略图并将该缩略图设置为 imageview。

public class AdapterForBooks extends ArrayAdapter<ModelFile> {

List<ModelFile> list;
Context context;
WindowManager wm;
Display display;
int w;

public AdapterForBooks(Context context, List<ModelFile> list) {
super(context, R.layout.list_layout_home, list);
// TODO Auto-generated constructor stub
this.list = list;
this.context = context;
wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
display = wm.getDefaultDisplay();
w = display.getWidth();

}

static class ViewHolder {
TextView txtName;
ImageView img;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = null;
if (convertView == null) {
LayoutInflater inflter = (LayoutInflater) LayoutInflater
.from(context);
view = inflter.inflate(R.layout.list_layout_home, null);
final ViewHolder holder = new ViewHolder();
holder.txtName = (TextView) view
.findViewById(R.id.textViewName);
holder.img = (ImageView) view.findViewById(R.id.imageView);
LayoutParams params = (LayoutParams) holder.img
.getLayoutParams();
params.height = w / 4;
params.width = w / 4;
holder.img.setLayoutParams(params);
view.setTag(holder);
} else {
view = convertView;
}
final ViewHolder holder = (ViewHolder) view.getTag();
String name = list.get(position).getFileName();
holder.txtName.setText(name);

String filePath = list.get(position).getFilePath();
// holder.img.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filePath),100,100,false));
new SetThumbnail()
.execute(new Thumbnail(holder, filePath, position));
return view;
}

class Thumbnail {
ViewHolder holder;
String path;
int position;

public Thumbnail(ViewHolder holder, String path, int position) {
this.holder = holder;
this.path = path;
this.position = position;
}

public ViewHolder getHolder() {
return holder;
}

public String getPath() {
return path;
}

public int getPosition() {
return position;
}

}

class SetThumbnail extends AsyncTask<Thumbnail, Void, Bitmap> {

ViewHolder holder;

int position;

@Override
protected Bitmap doInBackground(Thumbnail... params) {
// TODO Auto-generated method stub
int pos = params[0].getPosition();
position = pos;
String path = params[0].getPath();
holder = params[0].getHolder();

Log.e("Ref", "Pos = " + pos + " " + holder);
try {
Bitmap thumbnail = BitmapFactory.decodeFile(path);
thumbnail = Bitmap.createScaledBitmap(thumbnail, 100, 100,
false);

return thumbnail;
} catch (Exception e) {

return null;
}
}

@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);

if (holder != null && result != null) {

Log.i("Ref", "Position = " + position + " " + holder);
holder.img.setImageBitmap(result);
} else {
holder.img.setImageResource(R.drawable.format_picture);
}
}
}
}

我还可以在启动 asynktask 之前借助注释行设置缩略图,但这会减缓我的列表膨胀。

最佳答案

首先在类级别声明 inflater,在 getview 中只写 if condition if view is null 然后创建并运行它。它肯定会起作用。

   LayoutInflater inflter = (LayoutInflater) LayoutInflater
.from(context);
......
......

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {

view = inflter.inflate(R.layout.list_layout_home, null);
}

关于android - 滚动列表时 View 意外更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20866222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com