BasicGestureDetect / src / com.example.android.basicgesturedetect /

BasicGestureDetectFragment.java

1
/*
2
* Copyright (C) 2013 The Android Open Source Project
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
*      http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
 
17
package com.example.android.basicgesturedetect;
18
 
19
import android.content.Intent;
20
import android.os.Bundle;
21
import android.support.v4.app.Fragment;
22
import android.view.GestureDetector;
23
import android.view.MenuItem;
24
import android.view.MotionEvent;
25
import android.view.View;
26
 
27
import com.example.android.common.logger.Log;
28
import com.example.android.common.logger.LogFragment;
29
 
30
public class BasicGestureDetectFragment extends Fragment{
31
    @Override
32
    public void onCreate(Bundle savedInstanceState) {
33
        super.onCreate(savedInstanceState);
34
        setHasOptionsMenu(true);
35
    }
36
 
37
    @Override
38
    public void onActivityCreated(Bundle savedInstanceState) {
39
        super.onActivityCreated(savedInstanceState);
40
        View gestureView = getActivity().findViewById(R.id.sample_output);
41
        gestureView.setClickable(true);
42
        gestureView.setFocusable(true);
43
 
45
 
46
        // First create the GestureListener that will include all our callbacks.
47
        // Then create the GestureDetector, which takes that listener as an argument.
48
        GestureDetector.SimpleOnGestureListener gestureListener = new GestureListener();
49
        final GestureDetector gd = new GestureDetector(getActivity(), gestureListener);
50
 
51
        /* For the view where gestures will occur, create an onTouchListener that sends
52
         * all motion events to the gesture detector.  When the gesture detector
53
         * actually detects an event, it will use the callbacks you created in the
54
         * SimpleOnGestureListener to alert your application.
55
        */
56
 
57
        gestureView.setOnTouchListener(new View.OnTouchListener() {
58
            @Override
59
            public boolean onTouch(View view, MotionEvent motionEvent) {
60
                gd.onTouchEvent(motionEvent);
61
                return false;
62
            }
63
        });
65
    }
66
 
67
 
68
    @Override
69
    public boolean onOptionsItemSelected(MenuItem item) {
70
        if (item.getItemId() == R.id.sample_action) {
71
            clearLog();
72
        }
73
        return true;
74
    }
75
 
76
    public void clearLog() {
77
        LogFragment logFragment =  ((LogFragment) getActivity().getSupportFragmentManager()
78
                .findFragmentById(R.id.log_fragment));
79
        logFragment.getLogView().setText("");
80
    }
81
}