您的位置:首页 > 移动开发 > Android开发

Android之实现侧滑菜单(左侧)

2014-10-10 21:39 176 查看
只有忙完才有点时间来更新博客。今天要讲的是实现侧滑菜单,当然,这个我也是在借鉴了他人的开源项目后修改而成,个人认为还是可以的~~

首先是效果图:







原理很简单,我其实是继承LinearLayout来实现,当然还可以是其他布局类,这样做的好处是:

1、可以成为一个框架,只要你在xml文件里应用,则可以使你对应的Activity成为侧滑,左边放菜单,右边放你的内容,则可以成为侧滑菜单;

2、逻辑实现上简单,只需控制leftMargin即可。

难点是:对拦截事件的处理。

下面是原理图以及源码:



一、具体实现类

SlidingLayout.java

package t.first;

import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.LinearLayout;

/** 
 * @ 对外仅需设置的接口
 * 
 * -判断左侧布局是否完全显示出来,或完全隐藏,滑动过程中此值无效
 * boolean SlidingLayout.isLeftLayoutVisible() 
 * 
 * -将屏幕滚动到右侧布局界面
 * void SlidingLayout.scrollToRightLayout()
 * 
 * -将屏幕滚动到左侧布局界面	
 * void SlidingLayout.scrollToLeftLayout() 
 * 
 */

public class SlidingLayout extends LinearLayout {

	private static final int SNAP_VELOCITY = 200;  //滚动显示和隐藏左侧布局时,手指滑动需要达到的速度。	
	private VelocityTracker mVelocityTracker;      //用于计算手指滑动的速度。
	private int touchSlop;                         //在被判定为滚动之前用户手指可以移动的最大值。
    private int screenWidth;                       //屏幕宽度值
	private int leftEdge ;                         //左边最多可以滑动到的左边缘,值由左边布局的宽度来定,marginLeft到达此值之后,不能再减少。
    private int rightEdge = 0;                     //左边最多可以滑动到的右边缘,值恒为0,即marginLeft到达0之后,不能增加。
    private float xDown;                           //记录手指按下时的横坐标。
    private float yDown;                           //记录手指按下时的纵坐标。
    private float xMove;                           //记录手指移动时的横坐标。
    private float yMove;                           //记录手指移动时的纵坐标。
    private float xUp;                             //记录手机抬起时的横坐标。
    private boolean isLeftLayoutVisible;           //左侧布局当前是显示还是隐藏。只有完全显示或隐藏时才会更改此值,滑动过程中此值无效。
    private boolean isSliding;                     //是否正在滑动。
    private MarginLayoutParams leftLayoutParams;   //左侧布局的参数,通过此参数来重新确定左侧布局的宽度,以及更改leftMargin的值。
    private MarginLayoutParams rightLayoutParams;  //右侧布局的参数,通过此参数来重新确定右侧布局的宽度。
    private View leftLayout;                       //左侧布局对象。
    private View rightLayout;                      //右侧布局对象。
     
     
    //构造函数
	public SlidingLayout(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		
		
		//获取屏幕宽度
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);		
		screenWidth = wm.getDefaultDisplay().getWidth();
		
        //获取滑动的最短距离
		touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
				
		
	}

	//创建VelocityTracker对象,并将触摸事件加入到VelocityTracker当中。
    private void createVelocityTracker(MotionEvent event) 
	{
	    if (mVelocityTracker == null)
		{
			mVelocityTracker = VelocityTracker.obtain();
		}
			
		mVelocityTracker.addMovement(event);
	}
	
   
    
    //拦截触摸事件
    @Override  
    public boolean onInterceptTouchEvent(MotionEvent event) {  
         
    	
    	switch(event.getAction())
    	{  
        case MotionEvent.ACTION_DOWN:
        	xDown = event.getRawX();
			yDown = event.getRawY();
			
					
			//当左边菜单完全显示时,点击右边的View,我们希望是拦截,而左边不拦截
			if(xDown > leftLayout.getLeft()+leftLayout.getWidth() && isLeftLayoutVisible)
				return true;
			
        	break;  
        	
        case MotionEvent.ACTION_MOVE:  
        	
            int distanceX=(int) (event.getRawX()-xDown);
                                    
            //水平滑动距离超过一定距离,拦截所有子view的touch事件,来处理自身onTouch事件来达到滑动的目的  
            if(Math.abs(distanceX)>=touchSlop)  
            {            
                return true;                                  
            }  
            break;    	   
        }  
      	
        return false;  
    }  
    
    
    //触摸事件
    @Override
    public boolean onTouchEvent(MotionEvent event) 
	{
		// TODO 自动生成的方法存根
				
		createVelocityTracker(event);
				
		switch (event.getAction()) 
		{
		case MotionEvent.ACTION_DOWN:
			// 手指按下时,记录按下时的横坐标
			xDown = event.getRawX();
			yDown = event.getRawY();
					
			break;
					
		case MotionEvent.ACTION_MOVE:
			// 手指移动时,对比按下时的横坐标,计算出移动的距离,来调整右侧布局的leftMargin值,从而显示和隐藏左侧布局
			xMove = event.getRawX();
			yMove = event.getRawY();
			int distanceX = (int) (xMove - xDown);
			int distanceY = (int) (yMove - yDown);
			
			//只有触摸右边的View,才发生滑动
			if(xMove > leftLayout.getLeft()+leftLayout.getWidth()) 
			{
				
			//向右滑
			if (!isLeftLayoutVisible && distanceX >= touchSlop && (isSliding || Math.abs(distanceY) <= touchSlop)) 
			{
				isSliding = true;
						
				leftLayoutParams.leftMargin = leftEdge + distanceX;  
						
				if (leftLayoutParams.leftMargin > rightEdge) 
				{  
			          leftLayoutParams.leftMargin = rightEdge;  
			    }  
						  
			}
			//向左滑
		    else if (isLeftLayoutVisible && -distanceX >= touchSlop) 
			{
				isSliding = true;
						
				leftLayoutParams.leftMargin = distanceX; 
														
				if (leftLayoutParams.leftMargin < leftEdge) 
		        {  
			          leftLayoutParams.leftMargin = leftEdge;  
			    }
					
		    }
					
			leftLayout.setLayoutParams(leftLayoutParams);  
			
			}
					
		    break;  
					
					
		case MotionEvent.ACTION_UP:
			xUp = event.getRawX();
			int upDistanceX = (int) (xUp - xDown);
					
			if (isSliding) 
			{
				// 手指抬起时,进行判断当前手势的意图,从而决定是滚动到左侧布局,还是滚动到右侧布局
				if (wantToShowLeftLayout())
				{
					if (shouldScrollToLeftLayout())
					{
						scrollToLeftLayout();
					} 
					else
					{
						scrollToRightLayout();
					}
				} 
				else if (wantToShowRightLayout()) 
				{
					if (shouldScrollToRightLayout())
					{
						scrollToRightLayout();
					} 
					else 
					{
						scrollToLeftLayout();
					}
				}
			}
			//在左侧菜单完全显示时,我们希望的是点击右边的View可以发生恢复
			else if (upDistanceX < touchSlop && isLeftLayoutVisible && xUp > leftLayout.getLeft()+leftLayout.getWidth()) 
			{
				scrollToRightLayout();
			}
										
			recycleVelocityTracker(); 
					
			break;
					
		    }
	
			if (this.isEnabled()) 
			{
				 if (isSliding) 
				 {
				     unFocusBindView();
					 return true;
				 }
				 if (isLeftLayoutVisible) 
				 {
					 return true;
				 }
				 return false;
			}
			
			return true;
	}
    
    
	//将屏幕滚动到左侧布局界面,滚动速度设定为50.	
	public void scrollToLeftLayout() 
	{
		//传入第一个参数
		new ScrollTask().execute(50);
	}

	
	//将屏幕滚动到右侧布局界面,滚动速度设定为-50.
	public void scrollToRightLayout() 
	{
		//传入第一个参数
		new ScrollTask().execute(-50);
	}

	//左侧布局是否完全显示出来,或完全隐藏,滑动过程中此值无效。	 
	public boolean isLeftLayoutVisible() 
	{
		return isLeftLayoutVisible;
	}

	//在onLayout中重新设定左侧布局和右侧布局的参数。	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		super.onLayout(changed, l, t, r, b);
		
		if (changed) 
		{
						
			// 获取左侧布局对象  
            leftLayout = getChildAt(0);  
            leftLayoutParams = (MarginLayoutParams) leftLayout.getLayoutParams();  
            // 设置最左边距为负的左侧布局的宽度  
            leftEdge = -leftLayoutParams.width;  
            leftLayoutParams.leftMargin = leftEdge;  
            leftLayout.setLayoutParams(leftLayoutParams); 
            
            // 获取右侧布局对象  
            rightLayout = getChildAt(1);  
            rightLayoutParams = (MarginLayoutParams) rightLayout.getLayoutParams();  
            rightLayoutParams.width = screenWidth;  
            rightLayout.setLayoutParams(rightLayoutParams); 
            rightLayout.setClickable(true);
		
		}		
		
	}

	//判断当前手势的意图是不是想显示右侧布局。如果手指移动的距离是负数,且当前左侧布局是可见的,则认为当前手势是想要显示右侧布局。	
	private boolean wantToShowRightLayout()
	{
		return xUp - xDown < 0 && isLeftLayoutVisible;
	}

	
	//判断当前手势的意图是不是想显示左侧布局。如果手指移动的距离是正数,且当前左侧布局是不可见的,则认为当前手势是想要显示左侧布局。	
	private boolean wantToShowLeftLayout()
    {
		return xUp - xDown > 0 && !isLeftLayoutVisible;
	}

	//判断是否应该滚动将左侧布局展示出来。如果手指移动距离大于屏幕的1/2,或者手指移动速度大于SNAP_VELOCITY,就认为应该滚动将左侧布局展示出来。
	private boolean shouldScrollToLeftLayout() 
	{
		return xUp - xDown > leftLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY;
	}

	//判断是否应该滚动将右侧布局展示出来。如果手指移动距离加上leftLayoutPadding大于屏幕的1/2,或者手指移动速度大于SNAP_VELOCITY, 就认为应该滚动将右侧布局展示出来。
	private boolean shouldScrollToRightLayout() 
	{
		return xDown - xUp > leftLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY;
	}

		
	//获取手指在右侧布局的监听View上的滑动速度。	 
	private int getScrollVelocity() 
	{
		mVelocityTracker.computeCurrentVelocity(1000);
		int velocity = (int) mVelocityTracker.getXVelocity();
				
		return Math.abs(velocity);
	}

	//回收VelocityTracker对象。	
	private void recycleVelocityTracker() 
	{
		mVelocityTracker.recycle();
		mVelocityTracker = null;
	}

	
	//使用可以获得焦点的控件在滑动的时候失去焦点。
	private void unFocusBindView() 
	{
		if (rightLayout != null) 
		{
			rightLayout.setPressed(false);
			rightLayout.setFocusable(false);
			rightLayout.setFocusableInTouchMode(false);
		}
	}

	
//*********************************************************************************************************************
	
	/**
	 * @ 利用轻量级线程实现滑动 ,应用在手指松开的滑动动画
	 */
	class ScrollTask extends AsyncTask<Integer, Integer, Integer> 
	{

		//后台处理,入口参数对应第一个参数类型
		@Override
		protected Integer doInBackground(Integer... speed) {
						 
			int leftMargin = leftLayoutParams.leftMargin; 
						
			// 根据传入的速度来滚动界面,当滚动到达左边界或右边界时,跳出循环。
			while (true) 
			{
				
				leftMargin = leftMargin + speed[0];  
				if (leftMargin > rightEdge) 
				{  
                    leftMargin = rightEdge;  
                    break;  
                }  
                if (leftMargin < leftEdge) 
                {  
                    leftMargin = leftEdge;  
                    break;  
                } 
                
                //主动回调onProgressUpdate来更新界面(滑动)
                publishProgress(leftMargin);  
				
				//使当前线程睡眠指定的毫秒数,为了要有滚动效果产生,每次循环使线程睡眠10毫秒,这样肉眼才能够看到滚动动画。
				try 
				{
					Thread.sleep(10);
				} 
				catch (InterruptedException e) 
				{
					e.printStackTrace();
				}
				
			}
			if (speed[0] > 0) 
			{
				isLeftLayoutVisible = true;
			} 
			else 
			{
				isLeftLayoutVisible = false;
			}
			
			isSliding = false;
			
			//返回对应第三个参数类型,并且把值传入onPostExecute			
			return leftMargin;
		}

		//调用publishProgress时,回调这个方法,用来更新界面(滑动),入口参数对应第二个参数类型
		@Override
		protected void onProgressUpdate(Integer... leftMargin) 
		{
			
			leftLayoutParams.leftMargin = leftMargin[0];  
            leftLayout.setLayoutParams(leftLayoutParams);  
            
			//使用可以获得焦点的控件在滑动的时候失去焦点。
			unFocusBindView();
		}

		//在doInBackground执行完成后执行界面更新,入口参数对应第三个参数类型
		@Override
		protected void onPostExecute(Integer leftMargin) 
		{			
			leftLayoutParams.leftMargin = leftMargin;  
	        leftLayout.setLayoutParams(leftLayoutParams);  
		}
	}

}


以上就是关键代码,接下来,就可以运用到具体工程里面了,以下是示范代码:

layout\main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="horizontal"  >  
  
    <t.first.SlidingLayout  
        android:id="@+id/slidingLayout"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:orientation="horizontal" 
        >  
         
        <!-- 左侧菜单 -->
        <RelativeLayout
            android:id="@+id/menu"  
            android:layout_width="260dp" 
            android:layout_height="fill_parent"
            android:orientation="vertical"  
            android:background="#00ffcc">  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_centerInParent="true"
                android:text="This is menu"  
                android:textColor="#000000"  
                android:textSize="28sp" 
                />  
                          
       </RelativeLayout>  
        
       <!-- 右侧内容 -->
       <LinearLayout  
            android:id="@+id/content"               
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"  
            android:orientation="vertical">  
  
            <Button  
                android:id="@+id/menuButton"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:text="Menu" 
                />  
  
            <ListView  
                android:id="@+id/contentList"  
                android:layout_width="fill_parent"  
                android:layout_height="fill_parent" 
                />  
                         
        </LinearLayout>  
  
        
   
    </t.first.SlidingLayout>  
  
</LinearLayout>


MainActivity.java

package t.first;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
		
	private SlidingLayout slidingLayout;  //侧滑布局对象,用于通过手指滑动将左侧的菜单布局进行显示或隐藏。
    private Button menuButton;  //menu按钮,点击按钮展示左侧布局,再点击一次隐藏左侧布局。
    private ListView contentListView;//放在content布局中的ListView。
    
 	private ArrayAdapter<String> contentListAdapter;//作用于contentListView的适配器。
	
	// 用于填充contentListAdapter的数据源。	 
    private String[] contentItems = { "Content Item 1", "Content Item 2", "Content Item 3",
			"Content Item 4", "Content Item 5", "Content Item 6", "Content Item 7",
			"Content Item 8", "Content Item 9", "Content Item 10", "Content Item 11",
			"Content Item 12", "Content Item 13", "Content Item 14", "Content Item 15",
			"Content Item 16" };
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        slidingLayout = (SlidingLayout) findViewById(R.id.slidingLayout);
		menuButton = (Button) findViewById(R.id.menuButton);
		contentListView = (ListView) findViewById(R.id.contentList);		
		    
		contentListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contentItems);
		contentListView.setAdapter(contentListAdapter);
		
			
		menuButton.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
				
				if (slidingLayout.isLeftLayoutVisible())
				{
					slidingLayout.scrollToRightLayout();
				} 
				else 
				{
					slidingLayout.scrollToLeftLayout();
				}
			}
		});
		
		contentListView.setOnItemClickListener(new OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				
				String text = contentItems[position];
				Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
				
			}
		});
    }
}


以上就是今天的全部内容啦~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐