TouchListener{ ? ? ? ? ? private LinearLayout menuLayout;//菜单项 ? ? private LinearLayout contentLayout;//内容项 ? ? private LayoutParams? menuParams;//菜单项目的参数 ? ? private LayoutParams contentParams;//内容项目的参数contentLayout的宽度值? ? ? ? ? ? private int disPlayWidth;//手机屏幕分辨率 ? ? private float xDown;//手指点下去的横坐标 ? ? private float xMove;//手指移动的横坐标 ? ? private float xUp;//记录手指上抬后的横坐标 ? ? private float yDown;//手指点下去的纵坐标 ? ? private float yMove;//手指移动的纵坐标 ? ? ? ? ? private VelocityTracker mVelocityTracker; // 用于计算手指滑动的速度。? ? ? private float velocityX;//手指左右移动的速度 ? ? public static final int SNAP_VELOCITY = 400; //滚动显示和隐藏menu时,手指滑动需要达到的速度。? ? ? ? private boolean menuIsShow = false;//初始化菜单项不可翙 ? ? private static final int menuPadding=160;//menu完成显示,留给content的宽度 ? ? ? ? ? private ListView menuListView;//菜单列表的内容 ? ? private ScrollView scrollView;// 文本框的滚动条 ? ? private boolean wantToScrollText=false;//想要下下滚动文本内容 ? ? private boolean wantToScrollTextMenu=false; ? ? private boolean oneFucction=false;//确保函数只被调用一次 ? ? ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? requestWindowFeature(Window.FEATURE_NO_TITLE); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? initLayoutParams(); ? ? ? ? initMenuList(); ? ? ? ? initScrollView(); ? ? } ? /** ? *初始化Layout并设置其相应的参数 ? */ ? private void initLayoutParams() ? { ? ? //得到屏幕的大小? ? ? ? DisplayMetrics dm = new DisplayMetrics(); ? ? ? getWindowManager().getDefaultDisplay().getMetrics(dm);? ? ? ? disPlayWidth =dm.widthPixels;? ? ? ? ? ? ? ? //获得控件 ? ? ? menuLayout = (LinearLayout) findViewById(R.id.menu); ? ? ? contentLayout = (LinearLayout) findViewById(R.id.content); ? ? ? findViewById(R.id.layout).setOnTouchListener(this); ? ? ? ? ? ? ? //获得控件参数 ? ? ? menuParams=(LinearLayout.LayoutParams)menuLayout.getLayoutParams(); ? ? ? contentParams = (LinearLayout.LayoutParams) contentLayout.getLayoutParams(); ? ? ? ? ? ? ? //初始化菜单和内容的宽和边距 ? ? ? menuParams.width = disPlayWidth - menuPadding; ? ? ? menuParams.leftMargin = 0 - menuParams.width; ? ? ? contentParams.width = disPlayWidth; ? ? ? contentParams.leftMargin=0; ? ? ? ? ? ? ? //设置参数 ? ? ? menuLayout.setLayoutParams(menuParams); ? ? ? contentLayout.setLayoutParams(contentParams); ? ? ? ? ? } ? /** ? * 初始化菜单列表内容 ? */ ? private void initMenuList() ? {? ? ? final String[] strs = new String[] { "第1章 Java概述 ", "第2章 理解面向对象", "第3章 数据类型和运算符", "第4章 流程控制和数组", "第5章 面向对象(上)"}; ? ? ? menuListView = (ListView) findViewById(R.id.menuList); ? ? ? menuListView.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, strs));//为ListView绑定适配器 ? ? ? //启动列表点击监听事件? ? ? ? menuListView.setOnItemClickListener(new OnItemClickListener() {? ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onItemClick(AdapterView> arg0, View arg1, int arg2,long arg3) { ? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(),"您选择了" + strs[arg2], Toast.LENGTH_SHORT).show();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? }); ? ? ? ? ? ? }? ? /** ? * 初始化scrollView? ? */ ? public void initScrollView(){ ? ? ? scrollView = (ScrollView)this.findViewById(R.id.scrollview); ? ? ? scrollView.setOnTouchListener(this);//绑定监听侧滑事件的View,即在绑定的View进行滑动才可以显示和隐藏左侧布局。 这句非常重要,不要设置它的触摸事件 了,要不会吞掉布局的触摸事件 ? } ? ? @Override ? public boolean onTouch(View v, MotionEvent event) ? { ? ? ? acquireVelocityTracker(event); ? ? if (event.getAction()==MotionEvent.ACTION_DOWN) ? ? { ? ? ? ? ? xDown=event.getRawX();? ? ? ? ? ? yDown=event.getRawY(); ? ? ? ? ? return false; ? ? } ? ? else i |