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

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

下面看代码:

package com.zhy.view; 
 
import android.content.Context; 
import android.support.v4.widget.SlidingPaneLayout; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
 
public class FoldSlidingPanelLayout extends SlidingPaneLayout 
{ 
  public FoldSlidingPanelLayout(Context context, AttributeSet attrs) 
  { 
    super(context, attrs); 
  } 
  @Override 
  protected void onAttachedToWindow() 
  { 
    super.onAttachedToWindow(); 
     
    View child = getChildAt(0); 
    if (child != null) { 
 
      removeView(child); 
      final FoldLayout foldLayout = new FoldLayout(getContext()); 
      //foldLayout.setAnchor(0); 
      foldLayout.addView(child); 
      ViewGroup.LayoutParams layPar = child.getLayoutParams(); 
      addView(foldLayout, 0, layPar); 
       
      setPanelSlideListener(new PanelSlideListener() 
      { 
         
        @Override 
        public void onPanelSlide(View arg0, float arg1) 
        { 
          foldLayout.setFactor(arg1); 
        } 
         
        @Override 
        public void onPanelOpened(View arg0) 
        { 
          // TODO Auto-generated method stub 
           
        } 
         
        @Override 
        public void onPanelClosed(View arg0) 
        { 
           
        } 
      }); 
       
    } 
  } 
} 

我们继承了SlidingPaneLayout,然后在onAttachedToWindow中,取出侧滑的布局,在外层包上一个FoldLayout;并且在内部去监听setPanelSlideListener,在onPanelSlide种根据参数,去动态设置FoldLayout的factor.
2、测试
(1)、布局文件

<com.zhy.view.FoldSlidingPanelLayout xmlns:android="http://www.easck.com/apk/res/android" 
  xmlns:tools="http://www.easck.com/tools" 
  android:id="@+id/id_drawerLayout" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" > 
 
  <fragment 
    android:id="@+id/id_left_menu" 
    android:name="com.zhy.sample.folderlayout.LeftMenuFragment" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" /> 
 
  <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
 
    <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitCenter" 
      android:src="@drawable/xueshan" /> 
  </RelativeLayout> 
 
</com.zhy.view.FoldSlidingPanelLayout>