Saturday 16 January 2016

how to use onclick and ontouch listener both for diffrent functionality


There is code for different functionality on onclick and also different functionality for start and stop event of ontouch .

public class MyFragment extends BaseFragment implements View.OnTouchListener {

    @Override
    public boolean onTouch(final View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                prev_timestamp = System.currentTimeMillis();

            case MotionEvent.ACTION_UP:
                current_timestamp = System.currentTimeMillis();
                upcount++;
                if (current_timestamp - prev_timestamp < 250) {

                    if (upcount == 2) {
                        upcount = 0;
                        Log.d(TAG, "click event");
//here code for onClick event
                    } else {

                        new AsyncTask<Void, Void, Void>() {

                            @Override
                            protected Void doInBackground(Void... params) {
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                return null;
                            }

                            @Override
                            protected void onPostExecute(Void aVoid) {
                                if (upcount != 0) {
                                    TOUCH_END=false;
                                    Log.d(TAG, "touch start");
   //here code for onTouchStart event
                                }
                                super.onPostExecute(aVoid);
                            }
                        }.execute();

                    }
                } else {
                    upcount = 0;
                    Log.d(TAG, "touch end");
                    //here code for onTouchEnd event
                }

            case MotionEvent.ACTION_CANCEL:
                //here code for cancel event if you want

        }
        return true;
    }
}

4 comments:


  1. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this.
    android training in chennai

    ReplyDelete
  2. Hi, i really enjoyed to read your article.. i got clear idea through your views and ideas.. thanks for sharing your post..

    Android Training in chennai

    ReplyDelete
  3. Good one, very informative.. thanks for sharing your views and ideas.. it is very useful to me.. thanks once again

    Java Training in chennai | Software Testing Training in chennai

    ReplyDelete
  4. Good and nice blog post, thanks for sharing your information.. it is very useful to me.. keep rocks and updating..

    PHP Training in chennai

    ReplyDelete