【问题标题】:Android ListView: load items only on scrollAndroid ListView:仅在滚动时加载项目
【发布时间】:2014-03-04 19:11:18
【问题描述】:

我有一个用于列表视图的自定义适配器。它有 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;
}

【问题讨论】:

  • 此代码看起来可以满足您的要求。
  • @AdamS 是对的 getView 仅在列表视图需要屏幕上的视图时调用。
  • 谢谢@AdamS - 请发表答案,我会接受的

标签: android listview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多