我的listview有1个图像视图和2个文本视图但是当我向下滚动时它不能正常工作,所以我该怎么办?
现在这是我的代码..
class ReceiptAdapter extends BaseAdapter {
ArrayList<GetterSetter> receiptlist;
private LayoutInflater inflater;
// private int[] colors = new int[] { Color.parseColor("#C8A6DA"),
// Color.parseColor("#F6F4AB"), Color.parseColor("#A2C3D0"),
// Color.parseColor("#F1B4A1") };
private int[] colors = new int[] { Color.parseColor("#2280aee3"),
Color.parseColor("#22888888") };
public ReceiptAdapter(Context context,
ArrayList<GetterSetter> receiptlist) {
// TODO Auto-generated method stub
this.receiptlist = receiptlist;
// inflater = LayoutInflater.from(context);
// inflater = LayoutInflater.from(context.getApplicationContext());
inflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return receiptlist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
// convertView = inflater.inflate(R.layout.custom_list, null);
convertView = inflater.inflate(R.layout.custom_list, parent,
false);
// if (position % 2 == 1) {
// convertView.setBackgroundColor(Color.BLUE);
// } else {
// convertView.setBackgroundColor(Color.CYAN);
// }
int colorPos = position % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
holder = new ViewHolder();
holder.Title = (TextView) convertView.findViewById(R.id.name);
holder.Total = (TextView) convertView.findViewById(R.id.total);
holder.Img = (ImageView) convertView
.findViewById(R.id.profile_image);
Animation animation = null;
animation = AnimationUtils.loadAnimation(getActivity(),
R.anim.wave);
animation.setDuration(200);
convertView.startAnimation(animation);
animation = null;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.Title.setText(receiptlist.get(position).getTitle());
holder.Total.setText(receiptlist.get(position).getTotal());
String path = receiptlist.get(position).getImg();
File fileImg = new File(path);
Bitmap bitmap = null;
if (fileImg.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 7;
bitmap = BitmapFactory.decodeFile(fileImg.getAbsolutePath(),
options);
// bitmap = BitmapFactory.decodeFile(fileImg.getAbsolutePath());
holder.Img.setImageBitmap(bitmap);
}
else
{
Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(),
R.drawable.no_image);
holder.Img.setImageBitmap(icon);
}
holder.Img.setScaleType(ScaleType.CENTER_CROP);
return convertView;
}
class ViewHolder {
TextView Title;
TextView Total;
ImageView Img;
}
}
和我的异步任务,在postExecute()中我将自定义列表适配器称为,
ReceiptAdapter adapter = new ReceiptAdapter(getActivity(),
recList);
setListAdapter(adapter);
所以,我的问题是我哪里出错?
就像在联系人列表中的whatsapp一样,它在图像视图中加载图像和节目,我从SD卡中获取图像并在图像视图中显示,那么为什么滚动视图无法顺利运行?
我应该为此实施什么?
我也在stackoverflow上搜索了很多代码,但我发现其他人的错误相同,但没有得到任何好的可行解决方案,所以如果有人可以帮助我,那么我将不胜感激。谢谢你的语气!
答案 0 :(得分:0)
您需要在后台加载图片,而不是在主线程中加载。为此,您应该使用一个处理它的库。最好的图书馆是:
在实现其中一个库后,您应该使用它来在后台加载图像,这个库将图像缓存在内存中并处理其他选项,例如占位符用于错误下载,应用动画来显示图像等。
抱歉我的英文不好
答案 1 :(得分:0)
或者你可以使用Volley - 这是Google的标准REST库,它也能够加载图像。文档为here