package cn.vetech.demo.sliding_menu;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class HorzScrollWithImageMenu extends Activity {
public class MyGestureDetector extends SimpleOnGestureListener {
private double startpointer ;
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
startpointer = e.getX();
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
try {
distence = startpointer-e2.getX();
System.out.println(distence);
if(clickListenerForScrolling.isMenuOut()){
scrollView.smoothScrollTo((int) e1.getX() - (int) e2.getX(), 0);
}else{
scrollView.smoothScrollTo(SizeCallbackForMenu.wd+((int) e1.getX() - (int) e2.getX()), 0);
}
} catch (Exception e) {
Log.e("detector", "excetpion:" + e.getMessage());
}
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if(clickListenerForScrolling.isMenuOut()){
if(distence>SizeCallbackForMenu.wd/2){
clickListenerForScrolling.setMenuOut(true);
}else{
clickListenerForScrolling.setMenuOut(false);
}
clickListenerForScrolling.toScroll();
}else{
if(distence<-SizeCallbackForMenu.wd/2){
clickListenerForScrolling.setMenuOut(false);
}else{
clickListenerForScrolling.setMenuOut(true);
}
clickListenerForScrolling.toScroll();
}
}else{
if (gestureDetector.onTouchEvent(event))
return true;
}
return false;
}
private double distence;
MyHorizontalScrollView scrollView;
View menu;
View app;
ImageView btnSlide;
boolean menuOut = true;
int btnWidth;
private GestureDetector gestureDetector;
ListView.OnTouchListener gestureListener;
ClickListenerForScrolling clickListenerForScrolling;
View transparent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(this);
setContentView(inflater.inflate(R.layout.horz_scroll_with_image_menu,
null));
// MyHorizontalScrollView 自定义view
scrollView = (MyHorizontalScrollView) findViewById(R.id.myScrollView);
// @drawable/fb_menu黑色的那张图片
menu = findViewById(R.id.menu);
app = inflater.inflate(R.layout.horz_scroll_app, null);
// 点击滑动 tabBar包括图片和textview
ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);
btnSlide = (ImageView) tabBar.findViewById(R.id.BtnSlide);
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
clickListenerForScrolling = new ClickListenerForScrolling(scrollView,menu);
btnSlide.setOnClickListener(clickListenerForScrolling);
transparent = new TextView(this);
final View[] children = new View[] { transparent, app };
// 显示为图片的旁边的一道
scrollView.initViews(children,
new SizeCallbackForMenu(btnSlide));
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
handler.postDelayed(runnable, 500);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
}
};
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what == 1){
clickListenerForScrolling.toScroll();
}
}
};
}