Gallery.getChildAt(intposition) 的问题

发布于 2024-11-06 14:55:03 字数 2176 浏览 0 评论 0原文

我正在尝试在图库视图上创建预览效果,但遇到了 Gallery.getChildAt(intposition) 的问题,该问题大多数时候返回 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;
        }
    });

编辑:我不想填充图库视图,我已经完成了这部分并且非常了解 Adapter 类的使用。

编辑2:通过更改行 : 来解决

View child = gallery.getChildAt(position);

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

I'm trying to create a 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() );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-11-13 14:55:03

您的问题是,您假设 position 参数 getChildAt 所采用的位置和适配器中项目的位置相同 - 它们不是。

getChildAt 中的位置是子视图在其父 ViewGroup 中的位置,它与适配器位置无关 - 即它从 0 运行到 getChildCount()-1

您可以将其与 getFirstVisiblePosition() 结合使用,后者将返回适配器中第一个可见项的位置。第一个可见视图也是其父级中的第一个子级,因此您传递给该项目的 getChildAt 的位置将为 0

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.

鹊巢 2024-11-13 14:55:03

您无法像这样更改画廊项目(视图)(据我所知)。
您应该在库的适配器中更改此视图的数据并调用“notifyDataSetChange()”。在适配器的 getView 中,您可以根据数据更改视图的位图...(例如,您可以检查位图是否已加载,如果没有,则使用资源中的默认图像...)

如果这不是您想要的意思是请澄清。你使用什么样的适配器等...

祝你好运,
摩根大通

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文