开发者

Problems with Gallery.getChildAt(int position)

开发者 https://www.devze.com 2023-03-05 10:41 出处:网络
I\'m trying to create a 开发者_JS百科preview effect on a Gallery view and encountering issues with the Gallery.getChildAt(int position), which is returning null most of the time. It\'s supposed to ret

I'm trying to create a 开发者_JS百科preview effect on a Gallery view and encountering issues with the Gallery.getChildAt(int position), which is returning null most of the time. It's supposed to return null only when the child is not displayed, which is not the case here, since the user need to scroll over 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 populate the Gallery view, I'm already done with this part and well aware of the use of an Adapter class.

EDIT 2: Solved by changing the line :

View child = gallery.getChildAt(position);

with :

View child = gallery.getChildAt( position - gallery.getFirstVisiblePosition() );


Your issue is that you are assuming that the position argument getChildAt takes and the position of an item in your adapter are the same -- they are not.

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

You can use this in combination with getFirstVisiblePosition() which will return the position of the first item in your adapter which is visible. The first visible view is also the first child in it's parent so the position you'd pass to getChildAt for this item would be 0.


You cant change gallery items (views) like this (as far as I know). You should change the data of this view in the gallery's adapter and call "notifyDataSetChange()". In the adapter's getView you then can change the bitmap of the view according to the data... (for example you can check whether a bitmap is already loaded and if not use a default image from resources...)

If that's not what you meant please clarify. What kind of adapter do you use etc...

Good luck, JPM

0

精彩评论

暂无评论...
验证码 换一张
取 消