在触摸时列出视图位置,甚至始终返回0

时间:2014-05-22 09:49:04

标签: android android-listview touch-event

以下是我的代码: 但我的pos值为0。 请帮忙

 public boolean onTouch(View view, MotionEvent motionEvent) { 
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                pos = listView.pointToPosition((int) motionEvent.getX(), (int) motionEvent.getY());
                view.startDrag(null, shadowBuilder, view, 0);
                return true;
            }
}

1 个答案:

答案 0 :(得分:0)

按以下应用

中所示进行更改
DragShadowBuilder shadowBuilder;
public boolean onTouch(View view, MotionEvent motion)
       if (motionEvent.getAction() == Motion.ACTION_DOWN) {
             shadowBuilder = new View.DragShadowBuilder(view);
             pos = listView.pointToPosition((int) motion.getX(), (int) motion.getY());
             Toast.makeText(getApplicationContext(),"Pos is:"+pos,Toast.LENGTH_SHORT).show();
             view.startDrag(null, shadowBuilder, view, 0);
             return false;
       }
}