hee*_*ero 6 performance android horizontalscrollview
我有一个HorizontalScrollView,它包含一个LinearLayout来保存我的所有视图.我添加了大约20个RelativeLayout,它包含一个ImageView和TextView到LinearLayout.我想只在ImageView在屏幕上时加载图像(当我滚动到ImageView时).
我尝试在缩略图上使用此帖子getHitRect(),但缩略图的Rect(边界)始终为0,0-0,0,导致我的方法返回false.我究竟做错了什么?
thumbnailLayout是HorizontalScrollView里面的LinearLayout
thumbnailScroll是我的HorizontalScrollView
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e(TAG, "running");
for (int i = 0; i < thumbnailLayout.getChildCount(); i++) {
RelativeLayout view = (RelativeLayout) thumbnailLayout.getChildAt(i);
ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
if (thumbnailIsOnScreen(thumbnail)) {
Log.e(TAG, items.get(i).getTitle() + " has downloaded");
app.setImage(items.get(i).getThumbnailSmall(), thumbnail);
}
}
}
});
private boolean thumbnailIsOnScreen(ImageView thumbnail) {
Rect bounds = new Rect();
thumbnail.getHitRect(bounds);
Rect scrollBounds = new Rect(thumbnailScroll.getScrollX(), thumbnailScroll.getScrollY(),
thumbnailScroll.getScrollX() + thumbnailScroll.getWidth(), thumbnailScroll.getScrollY()
+ thumbnailScroll.getHeight());
return Rect.intersects(scrollBounds, bounds);
}
Run Code Online (Sandbox Code Playgroud)
编辑我使用TreeObserver并发现我的方法来检查是否在屏幕上显示缩略图是错误的.它仍然抓取所有图像,并不断循环(因为我正在使用onPreDrawListener?)
thumbnailLayout.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
Log.e(TAG, "running");
for (int i = 0; i < thumbnailLayout.getChildCount(); i++) {
RelativeLayout view = (RelativeLayout) thumbnailLayout.getChildAt(i);
ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
if (thumbnailIsOnScreen(thumbnail)) {
Log.e(TAG, items.get(i).getTitle() + " has downloaded");
app.setImage(items.get(i).getThumbnailSmall(), thumbnail);
}
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
你需要一个ListView,但android中没有原生的HorizontallListView。您可以尝试DevSmart 的 HorizontallListView。它的工作方式类似于本机列表视图,如果需要,可以使用适配器和自定义视图。
| 归档时间: |
|
| 查看次数: |
1040 次 |
| 最近记录: |