Android 如何在阵列适配器中实现视图保持器
*更新* 嘿伙计们我自己解决了我的问题我用 并传递urlmageviewHelper.setUrlDrawable(image,ei.img_url);来自我的Entryadapter类,它运行良好 我想在列表中显示包含3个文本的Listview和一个带有分隔符/节的imageview。 所以我遵循这个教程 然后,我面临加载图像(位图)的问题,所以我使用文件和位图工厂,通过使用AsyncTask在listview上显示位图 但问题是每次我向上或向下滚动时它都会加载。我想显示列表一次,所以我尝试为列表实现视图保持器,但由于我对android和Java比较新,我不知道如何在代码中实现这一点 有人能帮我吗?我附上所有的代码文件,所以这个代码将有助于其他学生和像我一样的新生 这是我的entryadapter.javaAndroid 如何在阵列适配器中实现视图保持器,android,android-layout,android-listview,Android,Android Layout,Android Listview,*更新* 嘿伙计们我自己解决了我的问题我用 并传递urlmageviewHelper.setUrlDrawable(image,ei.img_url);来自我的Entryadapter类,它运行良好 我想在列表中显示包含3个文本的Listview和一个带有分隔符/节的imageview。 所以我遵循这个教程 然后,我面临加载图像(位图)的问题,所以我使用文件和位图工厂,通过使用AsyncTask在listview上显示位图 但问题是每次我向上或向下滚动时它都会加载。我想显示列表一次,所以我尝试为
public class EntryAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
Uri myurl;
ImageView image;
public EntryAdapter(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else
{
EntryItem ei = (EntryItem)i;
v = vi.inflate(R.layout.list_item_entry, null);
final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);
image = (ImageView)v.findViewById(R.id.showlist_item_entry_drawable);
if (title != null)
title.setText(ei.title);
if(subtitle != null)
subtitle.setText(ei.subtitle);
if(image !=null)
{
try {
URL onLineURL = new URL(ei.img_url);
new MyNetworkTask(image).execute(onLineURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}}}}
return v;
}
private class MyNetworkTask extends AsyncTask<URL, Void, Bitmap>{
ImageView tIV;
public MyNetworkTask(ImageView iv){
tIV = iv;
}
@Override
protected Bitmap doInBackground(URL... urls) {
Bitmap networkBitmap = null;
URL networkUrl = urls[0]; //Load the first element
try {
networkBitmap = BitmapFactory.decodeStream(
networkUrl.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return networkBitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
tIV.setImageBitmap(result);
}
}
}
条目项目类别代码
public class EntryItem implements Item{
public final String title;
public final String subtitle;
public final String img_url;
public EntryItem(String title, String subtitle,String Image_url) {
this.title = title;
this.subtitle = subtitle;
this.img_url = Image_url;
}
public boolean isSection() {
return false;
}
}我建议将加载的位图缓存在某个地方,这样,如果您上下滚动(例如使用贴图),就可以避免服务器调用。 此外,由于要异步加载位图,因此加载数据后,请确保视图仍然是要将图像设置到的正确视图,因为视图在Listview中循环使用(因此称为“convertView”)。e、 g
嘿,伙计们,把这个库放到你的项目中,然后把图像视图对象和url传递给你,就完成了
为KoljaTM竖起大拇指。它解决了我的类似问题。我在gridview中使用线程来加速,我遇到了一些图片位置相互混合的问题。采用KoljaTM的方法后,没有出现扭曲。谢谢。我正在使用asynctask,因为每次findviewby id调用时,它都会使我的应用程序变慢,这就是我使用Asyntask的原因。当时我不知道如何使用view holder。视图持有者重用旧视图,除非更新列表,否则不会调用新视图。因此,我将删除async任务,您能建议我如何在代码中使用view holder吗?您肯定需要保留async任务以加载图像,否则您的应用程序将因为在UiThread上做了太多工作而强制关闭。我只是指出,你需要意识到,在此期间,情况可能会发生变化。在异步编程中,始终需要注意一件重要的事情。谢谢您能指导我如何使用MAP存储位图,以及如何避免下次使用findviewby id??根据您的解决方案,我必须将该位图存储到哈希映射中,然后我必须将其显示到listview中,因此当我浏览listview时,我如何编写它不会从中获取的代码web,但从存储的地图获取
public class SectionItem implements Item{
private final String title;
public SectionItem(String title) {
this.title = title;
}
public String getTitle(){
return title;
}
public boolean isSection() {
return true;
}
public class EntryItem implements Item{
public final String title;
public final String subtitle;
public final String img_url;
public EntryItem(String title, String subtitle,String Image_url) {
this.title = title;
this.subtitle = subtitle;
this.img_url = Image_url;
}
public boolean isSection() {
return false;
}
if (item.equals(imageView.getTag())) {
// only set image if Tag is still valid
imageView.setImageDrawable(drawable);
}