带有图标的Android抽屉始终可见

时间:2015-11-17 22:31:59

标签: android android-layout

哪个布局组件是用于构建带有始终可见图标的抽屉菜单的正确组件?我不想使用SlidingPaneLayout,因为主要内容不应受此影响。

enter image description here

1 个答案:

答案 0 :(得分:2)

1。添加CrossFadeSlidingPaneLayout

  public class CrossFadeSlidingPaneLayout extends SlidingPaneLayout {
  private View partialView = null;
  private View fullView = null;

  // helper flag pre honeycomb used in visibility and click response handling
  // helps avoid unnecessary layouts
  private boolean wasOpened = false;

  public CrossFadeSlidingPaneLayout(Context context) {
    super(context);
  }

  public CrossFadeSlidingPaneLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public CrossFadeSlidingPaneLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();

    if (getChildCount() < 1) {
      return;
    }

    View panel = getChildAt(0);
    if (!(panel instanceof ViewGroup)) {
      return;
    }

    ViewGroup viewGroup = (ViewGroup) panel;
    if (viewGroup.getChildCount() != 2) {
      return;
    }
    fullView = viewGroup.getChildAt(0);
    partialView = viewGroup.getChildAt(1);

    super.setPanelSlideListener(crossFadeListener);
  }

  @Override
  public void setPanelSlideListener(final PanelSlideListener listener) {
    if (listener == null) {
      super.setPanelSlideListener(crossFadeListener);
      return;
    }

    super.setPanelSlideListener(new PanelSlideListener() {
      @Override
      public void onPanelSlide(View panel, float slideOffset) {
        crossFadeListener.onPanelSlide(panel, slideOffset);
        listener.onPanelSlide(panel, slideOffset);
      }

      @Override
      public void onPanelOpened(View panel) {
        listener.onPanelOpened(panel);
      }

      @Override
      public void onPanelClosed(View panel) {
        listener.onPanelClosed(panel);
      }
    });
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    if (partialView != null) {
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        // "changed" means that views were added or removed
        // we need to move the partial view out of the way in any case (if it's supposed to of course)
        updatePartialViewVisibilityPreHoneycomb(isOpen());
      } else {
        partialView.setVisibility(isOpen() ? View.GONE : VISIBLE);
      }
    }
  }

  private SimplePanelSlideListener crossFadeListener = new SimplePanelSlideListener() {
    @Override
    public void onPanelSlide(View panel, float slideOffset) {
      super.onPanelSlide(panel, slideOffset);
      if (partialView == null || fullView == null) {
        return;
      }

      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        if (slideOffset == 1 && !wasOpened) {
          // the layout was just opened, move the partial view off screen
          updatePartialViewVisibilityPreHoneycomb(true);
          wasOpened = true;
        } else if (slideOffset < 1 && wasOpened) {
          // the layout just started to close, move the partial view back, so it can be shown animating
          updatePartialViewVisibilityPreHoneycomb(false);
          wasOpened = false;
        }
      } else {
        partialView.setVisibility(isOpen() ? View.GONE : VISIBLE);
      }

      ViewHelper.setAlpha(partialView, 1 - slideOffset);
      ViewHelper.setAlpha(fullView, slideOffset);
    }
  };

  private void updatePartialViewVisibilityPreHoneycomb(boolean slidingPaneOpened) {
    // below API 11 the top view must be moved so it does not consume clicks intended for the bottom view
    // this applies curiously even when setting its visibility to GONE
    // this might be due to platform limitations or it may have been introduced by NineOldAndroids library
    if (slidingPaneOpened) {
      partialView.layout(-partialView.getWidth(), 0, 0, partialView.getHeight());
    } else {
      partialView.layout(0, 0, partialView.getWidth(), partialView.getHeight());
    }
  }
}

2。活动布局

    <com.xxx.CrossFadeSlidingPaneLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sliding_pane_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="@dimen/side_pane_width"
        android:layout_height="match_parent"
        android:background="@color/purple">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="7dp"
                    android:background="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="Your Screen" />
            </LinearLayout>


            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="7dp"
                    android:background="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="Your Screen" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="7dp"
                    android:background="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="Your Screen" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="55dp"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/ic_launcher" />
        </LinearLayout>

    </FrameLayout>

    <TextView
        android:layout_width="@dimen/main_pane_width"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/partial_pane_width"
        android:layout_weight="1"
        android:background="@color/light_blue"
        android:text="@string/pane_2" />
</com.xxx.CrossFadeSlidingPaneLayout>

现在它的完成总是菜单可见:)

归属于原始项目:Github Url