-1

*更新 * 嘿伙计们,我自己解决了我的问题,我使用了https://github.com/koush/UrlImageViewHelper/blob/master/README.md 并通过 UrlImageViewHelper.setUrlDrawable(image,ei.img_url); 来自我的 Entryadapter 类,它运行良好。

我想在列表中显示带有 3 个文本和一个带有分隔符/部分的图像视图的 Listview。所以我按照本教程http://bartinger.at/listview-with-sectionsseparators/

比我面临加载图像(位图)的问题,所以我使用文件和 bitmapfactory 通过使用 AsyncTask 在列表视图上显示位图。

但问题是每次我向上或向下滚动时它都会加载。我想显示一次列表,所以我试图为我的列表实现视图持有者,但由于我是 android 和 Java 的新手,我不知道如何将它实现到我的代码。

谁能帮我?我附上了所有代码文件,因此此代码将有助于其他学生和像我这样的新生。

这是我的 Entryadaptor.java

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 ShowsFragment extends SherlockListFragment { ArrayList<Item> items = new ArrayList<Item>();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
     items.add(new SectionItem("Today"));
        items.add(new EntryItem("Item 1", "This is item 1.2","http://pierre.chachatelier.fr/programmation/images/mozodojo-original-image.jpg"));
        items.add(new EntryItem("Item 2", "This is item 1.3","http://upload.wikimedia.org/wikipedia/commons/8/85/Image-New_Delhi_Lotus.jpg"));
        items.add(new SectionItem("This Week"));
        items.add(new EntryItem("Item 4", "This is item 2.1","https://twimg0-a.akamaihd.net/profile_images/1080041262/VMIX_logo_zoom_bkbg1_normal.png")); EntryAdapter adapter = new EntryAdapter(getActivity(), items);
        setListAdapter(adapter);
    }
    public void onListItemClick(ListView l, View v, int position, long id) {
        if(!items.get(position).isSection()){
            EntryItem item = (EntryItem)items.get(position);
            Toast.makeText(getActivity(), "You clicked " + item.title , Toast.LENGTH_SHORT).show();
        }
        super.onListItemClick(l, v, position, id);
    }
}

部分项目代码

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;
}

}

4

3 回答 3

1

我建议在某处缓存加载的位图,以便在上下滚动时避免服务器调用(例如使用地图)。此外,由于您正在异步加载位图,一旦您加载了数据,请确保视图仍然是设置图像的正确视图,因为视图在 Listview 中被回收(因此名称为“convertView”)。例如

    if (item.equals(imageView.getTag())) {
        // only set image if Tag is still valid
        imageView.setImageDrawable(drawable);
    }
于 2012-11-08T15:42:18.473 回答
1

为 KoljaTM 竖起大拇指。它解决了我的类似问题。我在gridview中使用线程来加速,我遇到了一些图片位置相互混合的问题。采用 KoljaTM 的方法后,没有出现任何扭曲。谢谢。

于 2013-06-13T04:03:04.863 回答
0

嘿,伙计们,只需将此库放入您的项目中并传递图像视图对象和 url 并完成 https://github.com/koush/UrlImageViewHelper/blob/master/README.md

于 2012-11-09T10:07:40.490 回答