我已经阅读了很多关于这个问题的帖子,通常是因为在gridview中包含了可点击的对象,但这不是我的情况。我有一个带有imageview和gridview的framelayout,我在gridview中使用ontouchlistener移动它并且它可以工作,但是onItemClickListener不会触发。我的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是一个简单的framelayout,扩展为大于屏幕。我的gridview只有imageviews(8x8结构中的64)。在我使用gridview.setOnTouchListener移动myFrameLayout之前它工作得很好但是我已经摇了所以我改变了移动imageview和gridview,结果? bye bye shake和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
...
}
});
我认为我可以使用onTouchListener和事件ACTION_DOWN和ACTION_UP连续(没有ACTION_MOVE)手动获取gridview单元格计算的位置,但我想让它作为最后一个选项。
感谢您的帮助。
答案 0 :(得分:2)
最后我不得不在OnTouch内部手动执行OnItemClick。这是我使用的代码,方法adaptador.getPosition(gridview,(int)x,(int)m)返回屏幕上点击的选定位置。我尝试过使用pointtoposition(int,int)但是返回错误的位置。
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;
}
});