android 中从 URL 加载图像的正确方法是什么?
What is the correct way in android to load images from URL in a ListView without delay
我必须显示一个帖子列表,其中一些带有图片(想想 Twitter 之类的东西),当我在 ListView 上滚动时加载更多,但图片需要几秒钟才能出现。我怎样才能不拖延地做负载?目前我正在使用 Picasso
这是我的代码:
ListView lv = findViewById(R.id.list_feed);
arrayPosts = fillArray(data);
adapter= new PostAdapter(this, arrayPosts);
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
int lastIndexInScreen = visibleItemCount + firstVisibleItem;
if (lastIndexInScreen >= totalItemCount && totalItemCount > 0 && !isLoading) {
isLoading = true;
loadMore();
}
}
});
lv.setAdapter(adapter);
我的适配器的 getView() 方法:
public View getView(int i, View view, ViewGroup viewGroup) {
Post post = (Post) getItem(i);
view = LayoutInflater.from(context).inflate(R.layout.row_post_item, null);
(...)
Picasso.with(context).load(post.getImgProfile()).into(imgProfile);
Picasso.with(context).load(post.getImagePost()).into(imgPost);
(...)
return view;
}
我必须显示一个帖子列表,其中一些带有图片(想想 Twitter 之类的东西),当我在 ListView 上滚动时加载更多,但图片需要几秒钟才能出现。我怎样才能不拖延地做负载?目前我正在使用 Picasso
这是我的代码:
ListView lv = findViewById(R.id.list_feed);
arrayPosts = fillArray(data);
adapter= new PostAdapter(this, arrayPosts);
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
int lastIndexInScreen = visibleItemCount + firstVisibleItem;
if (lastIndexInScreen >= totalItemCount && totalItemCount > 0 && !isLoading) {
isLoading = true;
loadMore();
}
}
});
lv.setAdapter(adapter);
我的适配器的 getView() 方法:
public View getView(int i, View view, ViewGroup viewGroup) {
Post post = (Post) getItem(i);
view = LayoutInflater.from(context).inflate(R.layout.row_post_item, null);
(...)
Picasso.with(context).load(post.getImgProfile()).into(imgProfile);
Picasso.with(context).load(post.getImagePost()).into(imgPost);
(...)
return view;
}