Android中FoldingLayout折叠布局的用法及实战全攻略

2019-12-10 18:55:43于丽

看到这,大家可能会想,然后就和SlidingPaneLayout一样,写写布局文件就好了?其实不是的,如果你这么做了,你会发现侧滑很难拉出来,因为是这样的:
DraweLayout的侧滑菜单,比如我们拉出来50%,那么正常来说显示的时侧滑布局右侧的50%,但是这个0.5如果设置给我们的factor,它会把布局缩小到50%且在左边。
导致,你拉了50%其实还是上面都看不到,因为折叠到左侧的50%去了。这里依然有两种解决方案:
(1)、结合属性动画,做偏移,具体可参考:Android DrawerLayout 高仿QQ5.2双向侧滑菜单
(2)、让我们的折叠,收缩到最终的位置可以控制,我们现在统统往最坐标收缩,如果可以设置为最右边,那么本例就没有问题了。
2、引入anchor
我们引入一个mAnchor变量,值范围[0,1],控制FoldLayout最终折叠到的位置。其实修改的代码比较少,我贴一下修改的代码:

private void updateFold() 
  { 
     
    //... 
    float anchorPoint = mAnchor * w; 
    float midFold = (anchorPoint / mFlodWidth); 
 
    for (int i = 0; i < mNumOfFolds; i++) 
    { 
      //引入anchor 
      dst[0] = (anchorPoint > i * mFlodWidth) ? anchorPoint 
          + (i - midFold) * mTranslateDisPerFlod : anchorPoint 
          - (midFold - i) * mTranslateDisPerFlod; 
      dst[2] = (anchorPoint > (i + 1) * mFlodWidth) ? anchorPoint 
          + (i + 1 - midFold) * mTranslateDisPerFlod : anchorPoint 
          - (midFold - i - 1) * mTranslateDisPerFlod;                               }         

                                                                                                     }  
             

唯一改变的就是dst[0]和dst[2]的坐标,当然了,anchor引入以后,你需要判断原始的坐标是否小于anchorPoint,如果小于需要加一些偏移量,大于则反之。