android 列表视图滚动中没有平滑,就像 whatsapp 新聊天联系人列表一样
Smoothing is not there in android listview scrolling, like whatsapp new chat contact list does
我的列表视图有 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卡获取图像并在图像视图中显示,为什么滚动视图不能正常工作?
我应该为此实施什么?
我也在Whosebug上搜索了很多代码,但我发现了其他人的相同错误,但没有得到任何好的可行解决方案,所以如果有人能帮助我解决这个问题,那么我将不胜感激。谢谢一声!
您需要在后台加载图像,而不是在主线程中。为此,您应该使用处理该问题的库。最好的图书馆是:
- 毕加索http://square.github.io/picasso/
- 通用镜像下载https://github.com/nostra13/Android-Universal-Image-Loader
实现其中一个库后,您应该使用它在后台加载图像,该库在内存中缓存图像并处理其他选项,例如错误下载的占位符、应用动画显示图像等。
抱歉我的英语不好
或者您可以使用 Volley - 这是来自 Google 的标准 REST 库,它也能够加载图像。文档是 here
我的列表视图有 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卡获取图像并在图像视图中显示,为什么滚动视图不能正常工作?
我应该为此实施什么?
我也在Whosebug上搜索了很多代码,但我发现了其他人的相同错误,但没有得到任何好的可行解决方案,所以如果有人能帮助我解决这个问题,那么我将不胜感激。谢谢一声!
您需要在后台加载图像,而不是在主线程中。为此,您应该使用处理该问题的库。最好的图书馆是:
- 毕加索http://square.github.io/picasso/
- 通用镜像下载https://github.com/nostra13/Android-Universal-Image-Loader
实现其中一个库后,您应该使用它在后台加载图像,该库在内存中缓存图像并处理其他选项,例如错误下载的占位符、应用动画显示图像等。
抱歉我的英语不好
或者您可以使用 Volley - 这是来自 Google 的标准 REST 库,它也能够加载图像。文档是 here