Open top menu


 In this Blog explain how to used swipe gesture detector inside ListView in android.
the swipe gesture is an impotent part of user  interaction on user interface your android app.


package com.example.androidbegnnerpoint;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity  extends Activity {

  ListView tutorials;
String[] tutorial =
 { "1.SWIPE GETSTURE TUTORIAL",
"2.HOW TO BACK WEBVIEW TO ANDROID APP",
"3.LOAD WEBVIEW WITH PROGRESSBAR IN ANDROID",
"4.TABHOST WITH ACTIVITY IN ANDROID",
"5.DRAW CANVAS LINE ONTOUCH FINGER IN ANDROID",
"6.HOW TO SET DRAWABLE IMAGE IN TEXTVIEW PROGRAMATICALLY IN ANDROID",
"7.IMAGE SHADOW EFFECT (HIGHLIGHT/FOCUS) IN IMAGEVIEW IN ANDROID",
"8.GLOW EFFECT ON IMAGEVIEW IN ANDROID",
"9.DARG IMAGE ONTOUCH FINGER IN ANDROID",
"10.ANDROID BASIC CALCULATOR"};


 SwipeGestureListener gestureListener;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  tutorials= (ListView) findViewById(R.id.list_tutorials);
   ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
     MainActivity.this, android.R.layout.simple_list_item_1, tutorial);
 
   tutorials.setAdapter(arrayAdapter);
      gestureListener = new SwipeGestureListener(MainActivity.this);
      tutorials.setOnTouchListener(gestureListener);
 }



  class SwipeGestureListener extends SimpleOnGestureListener implements
    OnTouchListener {
   Context context;
   GestureDetector gestureDetector;
   static final int SWIPE_MIN_DISTANCE = 120;
   static final int SWIPE_MAX_OFF_PATH = 250;
   static final int SWIPE_THRESHOLD_VELOCITY = 200;

   public SwipeGestureListener() {
    super();
   }

   public SwipeGestureListener(Context context) {
    this(context, null);
   }

   public SwipeGestureListener(Context context, GestureDetector gestureDetector) {

    if (gestureDetector == null)
    gestureDetector = new GestureDetector(context, this);

    this.context = context;
    this.gestureDetector = gestureDetector;
   }

   @Override
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
     float velocityY) {

    final int position = tutorials.pointToPosition(
      Math.round(e1.getX()), Math.round(e1.getY()));

    String tutorialName = (String) tutorials.getItemAtPosition(position);

    if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
     if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH
       || Math.abs(velocityY) < SWIPE_THRESHOLD_VELOCITY) {
      return false;
     }
     if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
      Toast.makeText(MainActivity.this, "bottomToTop" + tutorialName,
        Toast.LENGTH_SHORT).show();
     } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
      Toast.makeText(MainActivity.this,
        "topToBottom  " + tutorialName, Toast.LENGTH_SHORT)
        .show();
     }
    } else {
     if (Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY) {
      return false;
     }
     if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
      Toast.makeText(MainActivity.this,
        "swipe RightToLeft " + tutorialName, 5000).show();
     } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
      Toast.makeText(MainActivity.this,
        "swipe LeftToright  " + tutorialName, 5000).show();
     }
    }

    return super.onFling(e1, e2, velocityX, velocityY);

   }

   @Override
   public boolean onTouch(View v, MotionEvent event) {

    return gestureDetector.onTouchEvent(event);
   }

   public GestureDetector getDetector() {
    return gestureDetector;
   }

  }

}


activity_main.xml
............................................................................................................................................................




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/layout">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logoweb" />

    <ListView
        android:id="@+id/list_tutorials"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          >
    </ListView>
</LinearLayout>
...............................................................................................................................................................






try this code...

0 comments