我正在尝试在 Gallery 视图上创建预览效果,并遇到 Gallery.getChildAt(int position) 的问题,该问题大部分时间都返回 null。它应该仅在不显示子项时才返回 null ,这里不是这种情况,因为用户需要滚动它。有没有办法解决这个问题,或者我应该改变我的方法?这是我的代码:
gallery.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
preview_container.setVisibility(View.GONE);
}
else if(event.getDownTime()>2500){
int position = gallery.pointToPosition((int)event.getX(), (int)event.getY());
if (position!=AdapterView.INVALID_POSITION){
View child = gallery.getChildAt(position);
if(child!=null){
Bitmap tmp_bitmap = book.imageForPreview(position);
previewImg.setImageBitmap(tmp_bitmap);
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(tmp_bitmap.getWidth()+2, tmp_bitmap.getHeight()+2,Gravity.NO_GRAVITY);
int[] coord = new int[2];
child.getLocationOnScreen(coord);
int y = MMBookReader.this.getWindowManager().getDefaultDisplay().getHeight()-(tmp_bitmap.getHeight()+view.getHeight());
int x = (int) coord[0]-(tmp_bitmap.getWidth()-child.getWidth())/2;
layout.leftMargin=x;
layout.topMargin=y;
preview_container.setVisibility(View.VISIBLE);
preview_container.setLayoutParams(layout);
previewPageCount.setText(book.getMmpages().get(position).getPosition()+"/"+book.getPages());
}
}
else
preview_container.setVisibility(View.GONE);
}
return false;
}
});
编辑:我不是试图填充画廊视图,我已经完成了这部分并且很清楚适配器类的使用。
编辑2:通过更改行解决:
View child = gallery.getChildAt(position);
和 :
View child = gallery.getChildAt( position - gallery.getFirstVisiblePosition() );