download: http://www.mediafire.com/download/kiop9wadi05pz9k/layer.tar.gz
package com.coboltforge.slidemenuexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.coboltforge.slidemenu.SlideMenu;
import com.coboltforge.slidemenu.SlideMenu.SlideMenuItem;
import com.coboltforge.slidemenu.SlideMenuInterface.OnSlideMenuItemClickListener;
public class MainActivity extends Activity implements
OnSlideMenuItemClickListener {
private SlideMenu slidemenu;
private final static int MYITEMID = 42;
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 1;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
RelativeLayout rlContainer;
Float previousX = 0f , currentX = 0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rlContainer = (RelativeLayout) findViewById(R.id.rlContainer);
/*
* There are two ways to add the slide menu: From code or to inflate it
* from XML (then you have to declare it in the activities layout XML)
*/
// this is from code. no XML declaration necessary, but you won't get
// state restored after rotation.
// slidemenu = new SlideMenu(this, R.menu.slide, this, 333);
// this inflates the menu from XML. open/closed state will be restored
// after rotation, but you'll have to call init.
slidemenu = (SlideMenu) findViewById(R.id.slideMenu);
slidemenu.init(this, R.menu.slide, this, 333);
// this can set the menu to initially shown instead of hidden
// slidemenu.setAsShown();
// set optional header image
slidemenu.setHeaderImage(getResources().getDrawable(
R.drawable.ic_launcher));
// this demonstrates how to dynamically add menu items
SlideMenuItem item = new SlideMenuItem();
item.id = MYITEMID;
item.icon = getResources().getDrawable(R.drawable.ic_launcher);
item.label = "Dynamically added item";
slidemenu.addMenuItem(item);
slidemenu.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
previousX = event.getX();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
currentX = event.getX();
Log.e("----previousX="+previousX, "currentX="+currentX);
if (previousX - currentX > SWIPE_MIN_DISTANCE )
{
slidemenu.show();
slidemenu.hide();
} else if (currentX - previousX > SWIPE_MIN_DISTANCE) {
slidemenu.show();
}
break;
}
return true;
}
});
// Gesture detection
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
// connect the fallback button in case there is no ActionBar
Button b = (Button) findViewById(R.id.buttonMenu);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// slidemenu.show();
}
});
// b.setOnTouchListener(gestureListener);
}
@Override
public void onSlideMenuItemClick(int itemId) {
switch (itemId) {
case R.id.item_one:
Toast.makeText(this, "Item one selected", Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
case R.id.item_two:
Toast.makeText(this, "Item two selected", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_three:
Toast.makeText(this, "Item three selected", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_four:
Toast.makeText(this, "Item four selected", Toast.LENGTH_SHORT)
.show();
break;
case MYITEMID:
Toast.makeText(this, "Dynamically added item selected",
Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // this is the app icon of the actionbar
slidemenu.show();
break;
}
return super.onOptionsItemSelected(item);
}
// ////////////////////////////////////
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.e("in", "fling");
try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
slidemenu.show();
slidemenu.hide();
Toast.makeText(MainActivity.this, "Left Swipe",
Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
slidemenu.show();
Toast.makeText(MainActivity.this, "Right Swipe",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
}
package com.coboltforge.slidemenuexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.coboltforge.slidemenu.SlideMenu;
import com.coboltforge.slidemenu.SlideMenu.SlideMenuItem;
import com.coboltforge.slidemenu.SlideMenuInterface.OnSlideMenuItemClickListener;
public class MainActivity extends Activity implements
OnSlideMenuItemClickListener {
private SlideMenu slidemenu;
private final static int MYITEMID = 42;
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 1;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
RelativeLayout rlContainer;
Float previousX = 0f , currentX = 0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rlContainer = (RelativeLayout) findViewById(R.id.rlContainer);
/*
* There are two ways to add the slide menu: From code or to inflate it
* from XML (then you have to declare it in the activities layout XML)
*/
// this is from code. no XML declaration necessary, but you won't get
// state restored after rotation.
// slidemenu = new SlideMenu(this, R.menu.slide, this, 333);
// this inflates the menu from XML. open/closed state will be restored
// after rotation, but you'll have to call init.
slidemenu = (SlideMenu) findViewById(R.id.slideMenu);
slidemenu.init(this, R.menu.slide, this, 333);
// this can set the menu to initially shown instead of hidden
// slidemenu.setAsShown();
// set optional header image
slidemenu.setHeaderImage(getResources().getDrawable(
R.drawable.ic_launcher));
// this demonstrates how to dynamically add menu items
SlideMenuItem item = new SlideMenuItem();
item.id = MYITEMID;
item.icon = getResources().getDrawable(R.drawable.ic_launcher);
item.label = "Dynamically added item";
slidemenu.addMenuItem(item);
slidemenu.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
previousX = event.getX();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
currentX = event.getX();
Log.e("----previousX="+previousX, "currentX="+currentX);
if (previousX - currentX > SWIPE_MIN_DISTANCE )
{
slidemenu.show();
slidemenu.hide();
} else if (currentX - previousX > SWIPE_MIN_DISTANCE) {
slidemenu.show();
}
break;
}
return true;
}
});
// Gesture detection
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
// connect the fallback button in case there is no ActionBar
Button b = (Button) findViewById(R.id.buttonMenu);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// slidemenu.show();
}
});
// b.setOnTouchListener(gestureListener);
}
@Override
public void onSlideMenuItemClick(int itemId) {
switch (itemId) {
case R.id.item_one:
Toast.makeText(this, "Item one selected", Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
case R.id.item_two:
Toast.makeText(this, "Item two selected", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_three:
Toast.makeText(this, "Item three selected", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_four:
Toast.makeText(this, "Item four selected", Toast.LENGTH_SHORT)
.show();
break;
case MYITEMID:
Toast.makeText(this, "Dynamically added item selected",
Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // this is the app icon of the actionbar
slidemenu.show();
break;
}
return super.onOptionsItemSelected(item);
}
// ////////////////////////////////////
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.e("in", "fling");
try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
slidemenu.show();
slidemenu.hide();
Toast.makeText(MainActivity.this, "Left Swipe",
Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
slidemenu.show();
Toast.makeText(MainActivity.this, "Right Swipe",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
}
No comments:
Post a Comment