Friday, January 10, 2014

Android ScrollView with GestureDetector

0 comments

The implementation of Android GestureDetector with ScrollView 



ScrollLayoutActivity


import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class ScrollLayoutActivity extends Activity {

 private static String TAG = ScrollLayoutActivity.class.getSimpleName();

 private LinearLayout mTopLayout;
 private LinearLayout mMiddleLayout;
 private LinearLayout mBottomLayout;

 private ScrollView mScrollView;

 private boolean possitionTop;
 private boolean possitionMiddle = true;

 int mLayoutHeight;
 float mDeviceHeight;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_testlayouts);

  DisplayMetrics displayMetrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

  // final float height=displayMetrics.heightPixels/displayMetrics.xdpi;

  // device height in pixels
  mDeviceHeight = displayMetrics.heightPixels;
  mLayoutHeight = (int) mDeviceHeight / 3;

  mScrollView = (ScrollView) findViewById(R.id.scrv);

  mTopLayout = (LinearLayout) findViewById(R.id.top);
  mMiddleLayout = (LinearLayout) findViewById(R.id.middle);
  mBottomLayout = (LinearLayout) findViewById(R.id.bottom);

  mTopLayout.setLayoutParams(new LinearLayout.LayoutParams(mTopLayout
    .getLayoutParams().width, mLayoutHeight));
  mBottomLayout.setLayoutParams(new LinearLayout.LayoutParams(
    mBottomLayout.getLayoutParams().width, mLayoutHeight));
  mMiddleLayout.setLayoutParams(new LinearLayout.LayoutParams(
    mMiddleLayout.getLayoutParams().width, (int) mDeviceHeight));

  mScrollView.setHorizontalFadingEdgeEnabled(false);
  mScrollView.setVerticalFadingEdgeEnabled(false);
  mScrollView.post(new Runnable() {
   public void run() {
    mScrollView.scrollTo(0, mLayoutHeight);
   }
  });

  // findViewById(R.id.button).setOnClickListener(new
  // View.OnClickListener() {
  //
  // @Override
  // public void onClick(View v) {
  // Toast.makeText(ScrollLayoutActivity.this, "height : " +
  // mDeviceHeight, Toast.LENGTH_SHORT).show();
  // }
  // });

  Toast.makeText(this, "Device Height : " + mDeviceHeight,
    Toast.LENGTH_SHORT).show();

 }

 public boolean dispatchTouchEvent(MotionEvent ev) {
  return mGestureDetector.onTouchEvent(ev);
 }

 SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   String swipe = "";
   float sensitvity = 100;

   if ((e1.getX() - e2.getX()) > sensitvity) {
    swipe += "Swipe Left\n";
   } else if ((e2.getX() - e1.getX()) > sensitvity) {
    swipe += "Swipe Right\n";
   } else {
    swipe += "\n";
   }

   if ((e1.getY() - e2.getY()) > sensitvity) {
    swipe += "Swipe Up\n";
    if (!possitionMiddle && possitionTop) {
     mScrollView.scrollBy(0, mLayoutHeight);
     possitionTop = false;
     possitionMiddle = true;
    } else if (possitionMiddle && !possitionTop) {
     mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
     possitionTop = false;
     possitionMiddle = false;
    }
   } else if ((e2.getY() - e1.getY()) > sensitvity) {
    swipe += "Swipe Down\n";
    if (possitionMiddle && !possitionTop) {
     mScrollView.fullScroll(ScrollView.FOCUS_UP);
     possitionTop = true;
     possitionMiddle = false;
    }
    if (!possitionMiddle && !possitionTop) {
     mScrollView
       .scrollTo(mScrollView.getBottom(), mLayoutHeight);
     possitionTop = false;
     possitionMiddle = true;
    }
   } else {
    swipe += "\n";
   }
   Log.d(TAG, swipe);
   return super.onFling(e1, e2, velocityX, velocityY);
  }

 };

 GestureDetector mGestureDetector = new GestureDetector(
   simpleOnGestureListener);
}




activity_scroll_layout.xml 



<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrv"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/container2"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="#1E1E1E"
        android:baselineAligned="false"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/top"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/black"
            android:baselineAligned="false"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/middle"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/white"
            android:baselineAligned="false"
            android:orientation="vertical" >

            <Button
                android:id="@+id/button"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/darker_gray"
            android:baselineAligned="false"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

</ScrollView>

Read more...