0

I have read many post about this problem, usually due to the inclusion of clickable objects in the gridview but that's not my case. I have a framelayout with a imageview and a gridview, I moves both using ontouchlistener in gridview and it's works, but onItemClickListener don't trigger. My xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/layoutJuego" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <paricio.toni.caththemole.MyFrameLayout android:id="@+id/frame" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > <ImageView android:id="@+id/fondo" android:layout_width="760dp" android:layout_height="760dp" android:layout_gravity="center" android:contentDescription="@android:string/untitled" android:scaleType="fitXY" android:src="@drawable/terrenoarena" /> <GridView android:id="@+id/gridView1" android:layout_width="640dp" android:layout_height="640dp" android:layout_gravity="center" android:columnWidth="80dp" android:horizontalSpacing="0dp" android:numColumns="8" android:scrollbars="none" android:stretchMode="columnWidth" android:verticalSpacing="0dp" > </GridView> </paricio.toni.caththemole.MyFrameLayout> 

MyFrameLayout is a simple framelayout extended to be bigger than screen. My gridview have only imageviews (64 in a 8x8 structure). It was working fine before when I used gridview.setOnTouchListener for moves myFrameLayout but I had shake so I changed to moves imageview and gridview, result? bye bye shake and bye bye onItemClickListener

 gridview = (GridView) findViewById(R.id.gridView1); adaptador = new ImageAdapter(this); gridview.setAdapter(adaptador); gridview.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int desplazamiento[]={0,0}; fondo.getLocationOnScreen(desplazamiento); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mx = event.getX(); my = event.getY(); return true; case MotionEvent.ACTION_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: 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; } }); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Log.i("miTraza","OnItemClick"); // Don't trigger ... } }); 

I think that I could get position for gridview cell calculating manually with onTouchListener and events ACTION_DOWN and ACTION_UP consecutives (without ACTION_MOVE) but I want let it as last option.

Thanks for your help.

4
  • 1
    Looks like you have overridden the touch event itself which means its you who has to handle click events to. Commented Sep 24, 2012 at 13:41
  • 1
    Have You tried return false for all events (for some You return true) from Yours OnTouchListener? Pass all touch events from where directly to gridview? Commented Sep 24, 2012 at 13:43
  • sandrstar, thanks I did before modificate. If I put in false I get onClick fires but views are not moving together anymore. I try of work from here. Andro Selva thanks too, it's my last option. Commented Sep 24, 2012 at 13:56
  • I'm lost. With return true It moves well but I don't get trigger in OnItemClickListener, With ACTION_UP return false I moves well and OnItemClickListener triggered but position is wrong. Commented Sep 24, 2012 at 15:17

1 Answer 1

2

In the end I had to perform manually OnItemClick inside OnTouch. This is the code I used, the method adaptador.getPosition (gridview, (int) x, (int) m) returns the selected position on the screen click. I've tried using pointtoposition (int, int) but returns me the wrong position.

 gridview.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int desplazamiento[]={0,0}; fondo.getLocationOnScreen(desplazamiento); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: click = true; mx = event.getX(); my = event.getY(); return true; case MotionEvent.ACTION_MOVE: curX = event.getX(); curY = event.getY(); if (click && Math.abs(mx-curX)<5 && Math.abs(my-curY)<5) { Log.i("miTraza","movimiento pequeño"); click = true; return true; } else { click = false; } 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: if (click) { int position = adaptador.getPosition(gridview, (int)mx, (int)my); if (position>=0 && position<=63) { gridview.performItemClick(gridview, position, 0); } click = false; } return true; } return false; } }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Use gridview.performItemClick(gridView.getChildAt(position), position, 0); instead of gridview.performItemClick(gridview, position, 0);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.