简体   繁体   English

在gridView中滚动以使视图抖动

[英]Scroll in gridView make view shake

I have a layout (which extends a framelayout) with a gridview and an imageview. 我有一个带有gridview和imageview的布局(扩展了framelayout)。 I scroll through the layout with an onTouchListener,. 我使用onTouchListener滚动浏览布局。 In my Galaxy Nexus it works fine, but in AVD and other devices (Galaxy SII for example) the view shakes and I can't find out why. 在我的Galaxy Nexus中,它可以正常工作,但是在AVD和其他设备(例如,Galaxy SII)中,视图会晃动,我找不到原因。

XML XML格式

<paricio.toni.caththemole.MyFrameLayout
    android:id="@+id/campo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/zonaMarcador" >
    <ImageView
        android:id="@+id/fondo"
        android:layout_width="800dp"
        android:layout_height="800dp"
        android:contentDescription="@android:string/untitled"
        android:src="@drawable/terrenoarena" />
    <GridView
        android:id="@+id/gridView1"
        android:layout_width="640dp"
        android:layout_height="640dp"
        android:layout_gravity="center"
        android:clickable="true"
        android:columnWidth="80dp"
        android:horizontalSpacing="0dp"
        android:numColumns="8"
        android:overScrollMode="never"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:verticalSpacing="0dp" >

    </GridView>

And setOnTouchListener code: 和setOnTouchListener代码:

    gridview.setOnTouchListener(new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) {
            int desplazamiento[]={0,0};
            campo.getLocationOnScreen(desplazamiento);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mx = event.getX();
                    my = event.getY();
                    return false;
                case MotionEvent.ACTION_MOVE:
                    curX = event.getX();
                    curY = event.getY();
                    campo.scrollBy((int) (mx - curX), (int) (my - curY));                 
                    mx = curX;
                    my = curY;
                    return false;
                case MotionEvent.ACTION_UP:
                    curX = event.getX();
                    curY = event.getY();
                    campo.scrollBy((int) (mx - curX), (int) (my - curY));
                    return false;
            }
            return false;
        }
    });

MyFrameLayout is a FrameLayout extended to be bigger than the screen: MyFrameLayout是扩展为大于屏幕的FrameLayout:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(0, 0);
}

I can scroll an ImageView in AVD without shake, but if I move the image and gridview (instead of the frame) the views move differently (and wrong). 我可以不晃动地在AVD中滚动ImageView,但是如果我移动图像和gridview(而不是框架),则视图的移动方式会不同(并且错误)。 I have tried many things but I can't avoid the shake. 我尝试了很多事情,但我无法避免这种震动。 ¿Any idea? 任何想法?

Thanks. 谢谢。

I solved shake moving contents: 我解决了摇动内容:

    gridview.setOnTouchListener(new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) {
            int desplazamiento[]={0,0};
            fondo.getLocationOnScreen(desplazamiento);
            Log.i("miTraza","touch event");
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.i("miTraza","touch event down");
                    mx = event.getX();
                    my = event.getY();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    Log.i("miTraza","touch move");
                    curX = event.getX();
                    curY = event.getY();
                    fondo.scrollBy((int) (mx - curX), (int) (my - curY));
                    gridview.scrollBy((int) (mx - curX), (int) (my - curY));
                    mx = curX;
                    my = curY;
                    return true;
                case MotionEvent.ACTION_UP:
                    Log.i("miTraza","touch up");
                    curX = event.getX();
                    curY = event.getY();
                    fondo.scrollBy((int) (mx - curX), (int) (my - curY));
                    gridview.scrollBy((int) (mx - curX), (int) (my - curY));
                    return true;
            }
            return false;
        }
    })

Now gridview and imageview moves together without shakes, I had a problem with onItemClick with gridview but this is another history. 现在,gridview和imageview一起移动而没有晃动,我在使用onview和gridview时遇到了问题,但这是另一个历史。

Thanks to android developer for your help. 感谢android开发人员的帮助。

what is it that you need for the gridview when scrolling ? 滚动时,gridview需要什么?

it seems that even though youv'e handled the touch events, you've chosen to return false to tell it that you haven't handled the touch event. 似乎即使您已经处理了触摸事件,您仍选择返回false来告诉您尚未处理触摸事件。

this means that it can do your code and its code when touching it . 这意味着它可以在触摸它时执行您的代码及其代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM