Android ListView:仅在滚动时加载项目

时间:2014-03-04 19:11:18

标签: android listview

我有一个列表视图的自定义适配器。它有3个字段:缩略图和两个文本字段。

布局膨胀后,缩略图将从外部服务器下载。

如何更改功能,以便仅在用户向下滚动时下载图像。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Inflate the layout
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_single, parent, false);

    ImageView image = (ImageView) rowView.findViewById(R.id.picture);
    TextView firstline = (TextView) rowView.findViewById(R.id.firstline);
    TextView secondline = (TextView) rowView.findViewById(R.id.secondline);         

    // Get information about the report
    AnimalLocationLog current = objects.get(position);

    // Get all the important information
    String species = current.getSpecies();
    String sanitizedSpecies = MiscHelpers.sanitizeString(species);
    int reportId = objects.get(position).getTrackerId();

    // Construct the URL to the image
    String imageLocation = websiteUrl + sanitizedSpecies + separator + reportId + extension;

    // Display user report          
    downloader.download(imageLocation, image);          
    firstline.setText(species);
    secondline.setText("Spotted " + current.getDateTime().toString("yyyy-MM-dd H:m"));

    return rowView;
}

1 个答案:

答案 0 :(得分:0)

使用ListViewListView.setOnScrollListener()添加OnScrollListener,然后使用ScrollListener.onScroll()方法进行下载。您可以使用ListView.getLastVisiblePosition()来确定哪些列表项可见,哪些列表项不可见。