0

I have a layout (which extends a framelayout) with a gridview and an imageview. I scroll through the layout with an 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.

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:

    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:

@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). I have tried many things but I can't avoid the shake. ¿Any idea?

Thanks.

k0nig
  • 340
  • 4
  • 15

2 Answers2

1

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.

Thanks to android developer for your help.

k0nig
  • 340
  • 4
  • 15
0

what is it that you need for the gridview when scrolling ?

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.

this means that it can do your code and its code when touching it .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Thanks for answered. Return false no matter, before I had a break and I had shakes. I probe returns statements and copy paste this for mistake. It's for a game, I had a gridview biggest than screen and I want moves freely for see everything. – k0nig Sep 22 '12 at 22:23
  • sorry . i still don't understand what you try to do. are you sure the default behavior doesn't work well to your needs? – android developer Sep 22 '12 at 22:35
  • No, I think no, I tried before with horizontal and vertical scrolls but it wasn't useful. I need a free scroll for moves in all map, think in a minesweeper that can't fit on the screen. This is a capture of what i have: https://lh6.ggpht.com/LOukJjqdmnt83SW6FkkOcTEEHUPqmMynhkHhHY4mkAiBQVIJ8nR0v5ax5lLkezDjSw=h230 I wanna move freely in any direction with any zoom, this works perfectly in my Galaxy Nexus but fail in others. – k0nig Sep 22 '12 at 23:10
  • nice. aren't there any alternative solutions for this problem? – android developer Sep 23 '12 at 07:35
  • I try to find, I'm trying things but until now without result. The strange thing is that this works fine on my device so I have to find a new valid solution for any device. – k0nig Sep 23 '12 at 10:23
  • I'm trying to move the contents (gridview and imageview) in place of the container (framelayout) and appears to move more fluid in the AVD. Now all I have to get is them to move together without distancing each of other. – k0nig Sep 23 '12 at 15:27