zoukankan      html  css  js  c++  java
  • 侧滑面板(对viewGroup的自定义)

    额,好吧,最近一直在做侧滑的事情,到目前为止一共是学了三种方法了,一个是直接加第三方开源框架SlidingMenu,第二给是用DrawerLayout,今天这个是用谷歌官方提供的在新的support-v4中添加了Widget  Drawer layout等侧滑效果,即ViewDragHelper,这里简单分享一下ViewDragHelper的实现方法。


    ViewDragHelper.Callback mCallback = new ViewDragHelper.Callback() {
    		// c. 重写事件
    		
    		// 1. 根据返回结果决定当前child是否可以拖拽
    		// child 当前被拖拽的View
    		// pointerId 区分多点触摸的id
    		@Override
    		public boolean tryCaptureView(View child, int pointerId) {
    			Log.d(TAG, "tryCaptureView: " + child);
    			return true;
    		};
    		
    		@Override
    		public void onViewCaptured(View capturedChild, int activePointerId) {
    			Log.d(TAG, "onViewCaptured: " + capturedChild);
    			// 当capturedChild被捕获时,调用.
    			super.onViewCaptured(capturedChild, activePointerId);
    		}
    
    		@Override
    		public int getViewHorizontalDragRange(View child) {
    			// 返回拖拽的范围, 不对拖拽进行真正的限制. 仅仅决定了动画执行速度
    			return mRange;
    		}
    		
    		// 2. 根据建议值 修正将要移动到的(横向)位置   (重要)
    		// 此时没有发生真正的移动
    		public int clampViewPositionHorizontal(View child, int left, int dx) {
    			// child: 当前拖拽的View
    			// left 新的位置的建议值, dx 位置变化量
    			// left = oldLeft + dx;
    			Log.d(TAG, "clampViewPositionHorizontal: " 
    					+ "oldLeft: " + child.getLeft() + " dx: " + dx + " left: " +left);
    			
    			if(child == mMainContent){
    				left = fixLeft(left);
    			}
    			return left;
    		}
    
    		// 3. 当View位置改变的时候, 处理要做的事情 (更新状态, 伴随动画, 重绘界面)
    		// 此时,View已经发生了位置的改变
    		@Override
    		public void onViewPositionChanged(View changedView, int left, int top,
    				int dx, int dy) {
    			// changedView 改变位置的View
    			// left 新的左边值
    			// dx 水平方向变化量
    			super.onViewPositionChanged(changedView, left, top, dx, dy);
    			Log.d(TAG, "onViewPositionChanged: " + "left: " + left + " dx: " + dx);
    			
    			int newLeft = left;
    			if(changedView == mLeftContent){
    				// 把当前变化量传递给mMainContent
    				newLeft = mMainContent.getLeft() + dx;
    			}
    			
    			// 进行修正
    			newLeft = fixLeft(newLeft);
    			
    			if(changedView == mLeftContent) {
    				// 当左面板移动之后, 再强制放回去.
    				mLeftContent.layout(0, 0, 0 + mWidth, 0 + mHeight);
    				mMainContent.layout(newLeft, 0, newLeft + mWidth, 0 + mHeight);
    			}
    			// 更新状态,执行动画
    			dispatchDragEvent(newLeft);
    			
    			// 为了兼容低版本, 每次修改值之后, 进行重绘
    			invalidate();
    		}
    
    		// 4. 当View被释放的时候, 处理的事情(执行动画)
    		@Override
    		public void onViewReleased(View releasedChild, float xvel, float yvel) {
    			// View releasedChild 被释放的子View 
    			// float xvel 水平方向的速度, 向右为+
    			// float yvel 竖直方向的速度, 向下为+
    			Log.d(TAG, "onViewReleased: " + "xvel: " + xvel + " yvel: " + yvel);
    			super.onViewReleased(releasedChild, xvel, yvel);
    			
    			// 判断执行 关闭/开启
    			// 先考虑所有开启的情况,剩下的就都是关闭的情况
    			if(xvel == 0 && mMainContent.getLeft() > mRange / 2.0f){
    				open();
    			}else if (xvel > 0) {
    				open();
    			}else {
    				close();
    			}
    			
    		}
    


  • 相关阅读:
    python中os模块中文帮助
    TypeError: string indices must be integers, not str
    ValueError: multi-byte encodings are not supported
    Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
    Codeforces Round #620 (Div. 2)D(LIS,构造)
    Codeforces Round #619 (Div. 2)D(模拟)
    Codeforces Round #619 (Div. 2)C(构造,容斥)
    Educational Codeforces Round 82 (Rated for Div. 2)E(DP,序列自动机)
    Educational Codeforces Round 82 (Rated for Div. 2)D(模拟)
    【PAT甲级】1114 Family Property (25分)(并查集)
  • 原文地址:https://www.cnblogs.com/sdksdk0/p/5585110.html
Copyright © 2011-2022 走看看