Gallery ビューでプレビュー効果を作成しようとしていますが、ほとんどの場合 null を返す Gallery.getChildAt(int position) で問題が発生しています。ユーザーがスクロールする必要があるため、子が表示されていない場合にのみ 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;
}
});
編集: ギャラリー ビューに入力しようとしているわけではありません。この部分は既に完了しており、Adapter クラスの使用を十分に認識しています。
EDIT 2: 行を変更して解決:
View child = gallery.getChildAt(position);
と :
View child = gallery.getChildAt( position - gallery.getFirstVisiblePosition() );