示例#1
0
  /**
   * Instantiates a new SlidingMenu.
   *
   * @param context the associated Context
   * @param attrs the attrs
   * @param defStyle the def style
   */
  public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    LayoutParams behindParams =
        new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    mViewBehind = new CustomViewBehind(context);
    addView(mViewBehind, behindParams);
    LayoutParams aboveParams =
        new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    mViewAbove = new CustomViewAbove(context);
    addView(mViewAbove, aboveParams);
    // register the CustomViewBehind with the CustomViewAbove
    mViewAbove.setCustomViewBehind(mViewBehind);
    mViewBehind.setCustomViewAbove(mViewAbove);
    mViewAbove.setOnPageChangeListener(
        new OnPageChangeListener() {
          public static final int POSITION_OPEN = 0;
          public static final int POSITION_CLOSE = 1;

          public void onPageScrolled(
              int position, float positionOffset, int positionOffsetPixels) {}

          public void onPageSelected(int position) {
            if (position == POSITION_OPEN && mOpenListener != null) {
              mOpenListener.onOpen();
            } else if (position == POSITION_CLOSE && mCloseListener != null) {
              mCloseListener.onClose();
            }
          }
        });

    // now style everything!
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu);
    // set the above and behind views if defined in xml
    int mode = ta.getInt(R.styleable.SlidingMenu_mode, LEFT);
    setMode(mode);
    int viewAbove = ta.getResourceId(R.styleable.SlidingMenu_viewAbove, -1);
    if (viewAbove != -1) {
      setContent(viewAbove);
    } else {
      setContent(new FrameLayout(context));
    }
    int viewBehind = ta.getResourceId(R.styleable.SlidingMenu_viewBehind, -1);
    if (viewBehind != -1) {
      setMenu(viewBehind);
    } else {
      setMenu(new FrameLayout(context));
    }
    int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_touchModeAbove, TOUCHMODE_MARGIN);
    setTouchModeAbove(touchModeAbove);
    int touchModeBehind = ta.getInt(R.styleable.SlidingMenu_touchModeBehind, TOUCHMODE_MARGIN);
    setTouchModeBehind(touchModeBehind);

    int offsetBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindOffset, -1);
    int widthBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindWidth, -1);
    if (offsetBehind != -1 && widthBehind != -1)
      throw new IllegalStateException(
          "Cannot set both behindOffset and behindWidth for a SlidingMenu");
    else if (offsetBehind != -1) setBehindOffset(offsetBehind);
    else if (widthBehind != -1) setBehindWidth(widthBehind);
    else setBehindOffset(0);
    float scrollOffsetBehind = ta.getFloat(R.styleable.SlidingMenu_behindScrollScale, 0.33f);
    setBehindScrollScale(scrollOffsetBehind);
    int shadowRes = ta.getResourceId(R.styleable.SlidingMenu_shadowDrawable, -1);
    if (shadowRes != -1) {
      setShadowDrawable(shadowRes);
    }
    int shadowWidth = (int) ta.getDimension(R.styleable.SlidingMenu_shadowWidth, 0);
    setShadowWidth(shadowWidth);
    boolean fadeEnabled = ta.getBoolean(R.styleable.SlidingMenu_fadeEnabled, true);
    setFadeEnabled(fadeEnabled);
    float fadeDeg = ta.getFloat(R.styleable.SlidingMenu_fadeDegree, 0.33f);
    setFadeDegree(fadeDeg);
    boolean selectorEnabled = ta.getBoolean(R.styleable.SlidingMenu_selectorEnabled, false);
    setSelectorEnabled(selectorEnabled);
    int selectorRes = ta.getResourceId(R.styleable.SlidingMenu_selectorDrawable, -1);
    if (selectorRes != -1) setSelectorDrawable(selectorRes);
    ta.recycle();
  }