기본적으로 WebView는 onClickListener가 먹지 않는다. 그러므로 onTouchListener를 달아줘야 하는데 onTouchListener는 ACTION_DOWN, ACTION_MOVE, ACTION_UP을 구현해줘야 한다.







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
public class MainActivity extends Activity implements View.OnTouchListener{
 
    // 클릭으로 볼 최대시간        
    private static final int MAX_CLICK_DURATION = 1000;
    // 클릭으로 볼 최대거리 dp        
    private static final int MAX_CLICK_DISTANCE = 15;
 
    private long pressStartTime;
    private float pressedX;
    private float pressedY;
    private boolean stayedWithinClickDistance;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        findViewById(R.id.webview).setOnTouchListener(this);
    }
 
 
 
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                pressStartTime = System.currentTimeMillis();
                pressedX = motionEvent.getX();
                pressedY = motionEvent.getY();
                stayedWithinClickDistance = true;
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                if (stayedWithinClickDistance && distance(pressedX, pressedY, motionEvent.getX(), motionEvent.getY()) > MAX_CLICK_DISTANCE) {
                    stayedWithinClickDistance = false;
                }
                break;
            }
            case MotionEvent.ACTION_UP: {
                long pressDuration = System.currentTimeMillis() - pressStartTime;
                if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
                    // 클릭처리
                }
            }
        }
        return false;
    }
 
}
cs


+ Recent posts