有人可以建议我如何检测列表视图是否超出范围?我在列表视图中只有一项。而且我需要检测用户何时向上或向下滑动它并且列表视图相应地向上或向下并在其上方或下方显示空白空间。在这一刻,我应该更新我的列表视图的适配器并在那里传递新项目。
我尝试使用 GestureListener 来实现它,如下所示:
class MyGestureListener extends SimpleOnGestureListener implements OnTouchListener
{
public boolean onDown(MotionEvent arg0) {
InteractionsAdapter adapter = new InteractionsAdapter(InteractionsActivityAlt.this,
R.layout.interactions_item, DashboardActivityAlt.messagesExport,DashboardActivityAlt.messagesExport.size()-2);
adapter.notifyDataSetChanged();
Log.i("asd","sdas");
return true;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(Math.abs(e1.getY()-e2.getY()) > 250)
return false;
if(e1.getX() - e2.getX() > 120// && Math.abs(velocityX) > 200
){
//mSwipeDetected = Action.RL;
Log.d("one", "Coords: x=" + e1.getX() + ",y=" + e2.getY());
//do something...
return true;
}
else if(e2.getX() - e1.getX() > 120// && Math.abs(velocityX) > 200
){
// mSwipeDetected = Action.LR;
Log.d("two", "Coords: x=" + e1.getX() + ",y=" + e2.getY());
//do something...
return true;
}
return false;
}
public void onLongPress(MotionEvent arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(InteractionsActivityAlt.this);
builder.setMessage("Are you sure to choose timezone "+" ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float arg2,
float arg3) {
float pos = e2.getY() - e1.getY();
if (pos < -170 & pos > -200)
{
mSwipeDetected = Action.BT;
try {
Log.i("gesturelistener", "Move down");
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pos > 170 & pos < 200)
{
mSwipeDetected = Action.BT;
try {
Log.i("gesturelistener", "Move up");
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onTouchEvent(MotionEvent me)
{
return gestureScanner.onTouchEvent(me);
}
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_UP)
arg0.setBackgroundColor(Color.WHITE);
return false;
}
}
它检测向上或向下滑动动作,但不是当列表视图超出范围并显示空白空间时。