我目前在 Android 上工作,我创建了从 URL 加载图像的 listview
。我通过以下代码实现了这一点
InputStream is = (InputStream) new URL("http://ka.35pk.com/uploadfiles/gamepic/090830121252.jpg").getContent();
Drawable d = Drawable.createFromStream(is, "pic name");
imageview.setImageDrawable(d);
在 listview
中,直到我向下滚动一次 listview
图像才会加载。但它可以正确加载到 listview
的不可见部分。即,如果我在 listview 上有 100 张图片意味着,一次只有 10 张图片在屏幕上可见,这 10 张图片无法在请求时加载,当我向下滚动时,另外 10 张不可见的图片现在出现在可见部分,并且这些图像现在已成功加载,当我再次向上滚动时,现在那些未加载的图像现在也已加载,这意味着,当它在屏幕上可见时无法加载。对不起我的英语。希望,我已经详细解释了。如何在不使用向下/向上滚动的情况下从 URL 加载所有 listview
图像。请帮助我。谢谢。
最佳答案
检查这个答案对你有帮助:
和
如果您的应用程序有很多图像并且还存在内存问题,那么您必须以自己的方式处理。
喜欢使用延迟加载在 ListView 中下载图像。对于其他图像,请使用另一种方式,如链接 2。
对于您的滚动问题,请检查您在适配器中添加的 getView()
代码,如下所示:
public class ListViewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ListViewAdapter(Context con) {
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(con);
}
public int getCount() {
// TODO Auto-generated method stub
return SoapParser.title_date.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
// return product_id1.size();
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
// return product_id1.get(position).hashCode();
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.row_tb, null);
holder = new ListContent();
holder.date = (TextView) v.findViewById(R.id.textView1);
holder.actual = (TextView) v.findViewById(R.id.textView2);
holder.targate = (TextView) v.findViewById(R.id.textView3);
v.setTag(holder);
} else {
holder = (ListContent) v.getTag();
}
holder.date.setId(position);
holder.actual.setId(position);
holder.targate.setId(position);
holder.date.setText(" " + SoapParser.title_date.get(position));
holder.actual.setText(" " + SoapParser.title_actual.get(position));
holder.targate
.setText(" " + SoapParser.title_targate.get(position));
return v;
}
}
如果您在布局中使用任何其他 View ,则使其成为 android:focusable='false'
关于android - 图像未从 ListView 上的 URL 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14134677/