Problems with Gallery.getChildAt (int position)

I am trying to create a preview effect in the Gallery view and running into problems with Gallery.getChildAt (int position), which in most cases returns zero. It should return null only when the child is not displayed, which is not the case here, since the user needs to scroll through it. Is there a way to fix this, or should I change my approach? Here is my code:

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; } }); 

EDIT: I'm not trying to fill out the Gallery view, I already did this with this part and was well aware of using the adapter class.

EDIT 2: Solved by changing the line:

 View child = gallery.getChildAt(position); 

with:

 View child = gallery.getChildAt( position - gallery.getFirstVisiblePosition() ); 
+4
source share
2 answers

Your problem is that you think that the position argument getChildAt takes, and the position of the element in your adapter is the same - they are not.

The position in getChildAt is the position of the child in it of the parent ViewGroup, it has nothing to do with your adapter position - that is, it works from 0 to getChildCount()-1 .

You can use this in combination with getFirstVisiblePosition() , which will return the position of the first element in your adapter to be visible. The first visible view is also the first child in it by the parent, so the position you switched to getChildAt for this element will be 0 .

+8
source

You cannot change gallery elements (views) like this (as far as I know). You must change the data of this kind in the gallery adapter and call "notifyDataSetChange ()". In the getView adapter, you can change the bitmap of the view according to the data ... (for example, you can check if the bitmap is loaded and not use the default image from resources ...)

If this is not what you had in mind, make it clear. Which adapter are you using, etc.

Good luck, JPM

0
source

All Articles