Finger drag on motorola droid
I am programming an android application and my "testing" device is a Motorola Milestone (Droid). I have done a grid that scroll like the iPhone home menu ( with "points"). I got two problems:
- The first one : the drag only works on the Android Device Emulator and don't on the Droid! (Maybe the multi-touch screen is a problem?)
- The drag is too responsible, and flip views sometimes one by one (this is ok) and sometimes 2 by 2 or 3 by 3! That is clearly problematic!
Here is the code of my OnTouch method:
public boolean onTouch(View v, MotionEvent event) {
if (v instanceof GridView){
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_MOVE:
if (event.getEdgeFlags()==MotionEvent.EDGE_LEFT){
vf2.setInAnimation(this, R.anim.slide_left);
vf2.setOutAnimation(this, R.anim.slide_right);
vf2.showNext();
if (numCurrentPage==2){
numCurrentPage= 0;
} else {
numCurrentPage++;
}
notifyPageNumber(numCurrentPage);
}
if (event.getEdgeFlags()==MotionEvent.EDGE_RIGHT){
vf2.setInAnimation(this, R.anim.slide_in);
vf2.setOutAnimation(this, R.anim.slide_out);
vf2.showPrevious();
if (numCurrentPage==0){
numCurrentPage= 2;
} else {
numCurrentPage--;
}
notifyPageNumber(numCurrentPage);
}
break;
default:
break;
开发者_JAVA技巧 }
}
return false;
}
Thanks for your help!
Update : It doesn't work anymore on the Google Nexus One!
If you consider the onTouch event being handled you should return true to indicate that it should not propagate any further. If you return false other listeners will receive it and might do something about it, which could be causing this. How about trying:
if (v instanceof GridView){
...
return true;
}
return false;
Ok, I manage to have a good drag with this:
public boolean onTouch(View v, MotionEvent event) {
if (v instanceof GridView){
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
xStart = event.getX();
break;
case MotionEvent.ACTION_UP:
xEnd = event.getX();
System.out.println("Start = "+xStart+", End = "+xEnd);
if (xEnd - xStart > 100){
vf2.setInAnimation(this, R.anim.slide_in);
vf2.setOutAnimation(this, R.anim.slide_out);
vf2.showPrevious();
if (numPageActuelle==0){
numCurrentPage= 2;
} else {
numCurrentPage--;
}
notifyPageNumber(numCurrentPage);
}
if (xEnd - xStart < -100){
vf2.setInAnimation(this, R.anim.slide_left);
vf2.setOutAnimation(this, R.anim.slide_right);
vf2.showNext();
if (numPageActuelle==2){
numCurrentPage= 0;
} else {
numCurrentPage++;
}
notifyPageNumber(numCurrentPage);
}
return true;
default:
break;
}
return true;
}
return false;
}
Now, I get an other big problem : I can't click on my grid!
The code of my grid is :
private ViewFlipper vf;
......
vf.setOnTouchListener(this);
GridView pageGrid = new GridView(this);
pageGrid.setNumColumns(3);
pageGrid.setAdapter(new ModuleAdapter(this,listPages.get(j)));
pageGrid.setOnTouchListener(this);
vf.addView(pageGrid);
I don't now how to get again the clicks on my grid...
If someone have an idea...
精彩评论