Scroll in gridView make view shake

2019-09-11 19:29发布

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.

2条回答
走好不送
2楼-- · 2019-09-11 19:57

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.

查看更多
做自己的国王
3楼-- · 2019-09-11 20:07

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 .

查看更多
登录 后发表回答