[英]Android ListView: load items only on scroll
I have a custom adapter for a list view. 我有一个用于列表视图的自定义适配器。 It has 3 fields: a thumbnail and two text fields.
它具有3个字段:缩略图和两个文本字段。
Once the layout is inflated, the thumbnails are downloaded from an external server. 扩大布局后,便会从外部服务器下载缩略图。
How do i change the functionality so that the images are downloaded only when the user scrolls down. 我如何更改功能,以便仅在用户向下滚动时才下载图像。
@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;
}
Add an OnScrollListener to your ListView
with ListView.setOnScrollListener()
and do the download in the ScrollListener.onScroll()
method. 使用
ListView.setOnScrollListener()
将OnScrollListener添加到ListView
,并在ScrollListener.onScroll()
方法中进行下载。 You can use the ListView.getLastVisiblePosition()
to figure out what list items are visible and which aren't. 您可以使用
ListView.getLastVisiblePosition()
来确定哪些列表项可见,哪些不可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.