package com.rosa.test.slideview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.GestureDetector.OnGestureListener;
import android.widget.HorizontalScrollView;
public class MenuSlideView extends HorizontalScrollView{
private Context mContext;
private int mScreenWidth;
//Gesture
private GestureDetector mGesture;
private GestureDetector.OnGestureListener mGesturesListener = new OnGestureListener(){
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("ROSA", "start onDown");
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub
Log.d("ROSA", "start onFling");
if(velocityX > 0){
smoothScrollTo(0, 0);
}else{
smoothScrollTo(mScreenWidth, 0);
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("ROSA", "start onLongPress");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
Log.d("ROSA", "start onScroll");
smoothScrollBy((int)distanceX, 0);
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("ROSA", "start onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("ROSA", "start onSingleTapUp");
return false;
}
};
public MenuSlideView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mContext = context;
createSubView();
}
public MenuSlideView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mContext = context;
createSubView();
}
public MenuSlideView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
mContext = context;
createSubView();
}
private void createSubView(){
mGesture = new GestureDetector(mContext, mGesturesListener);
//scroll view setting
setHorizontalScrollBarEnabled(false);
//get screen size
Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
mScreenWidth = display.getWidth();
//set sub layout
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.menu_layout, null);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 100);
addView(v, params);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
mGesture.onTouchEvent(ev);
int action = ev.getAction();
switch(action){
case MotionEvent.ACTION_UP:
if(getScrollX() < mScreenWidth/2){
smoothScrollTo(0, 0);
}else{
smoothScrollTo(mScreenWidth, 0);
}
break;
}
return true;
}
}
|