按照Android中的Material设计创建ListPopUpWindow

时间:2017-06-30 20:30:26

标签: android material-design material

我试图在Android中创建一个选择菜单而不是使用Spinner,但我面临着布局的一些问题。如何使用ListPopUpWindow创建类似下图的内容?

由于

enter image description here

1 个答案:

答案 0 :(得分:1)

我真的很喜欢这些菜单漂浮在选定的项目上,所以我创建了自己的DropDown类来实现你的情况。它完全与@uguboz写的一样。

我正在使用带有RecyclerView的自定义布局的PopupWindow。然后我处理onClick,显示该窗口并使用重写PopupWindow.update()来计算正确的窗口位置。

最有趣的部分是这段代码:

public class DropDownMenu extends PopupWindow {

    public boolean show(View anchor) {
        mAnchorView = anchor;

        super.showAtLocation(anchor, Gravity.START | Gravity.TOP, 0, 0);

        update();

        return true;
    }

    public void update() {
        final Resources res = getContentView().getContext().getResources();

        int margin = (int) res.getDimension(R.dimen.carbon_margin);
        int itemHeight = (int) res.getDimension(R.dimen.carbon_listItemHeight);
        int marginHalf = (int) res.getDimension(R.dimen.carbon_paddingHalf);

        ArrayAdapter adapter = getAdapter();

        Rect windowRect = new Rect();
        mAnchorView.getWindowVisibleDisplayFrame(windowRect);
        int hWindow = windowRect.bottom - windowRect.top;
        int wWindow = windowRect.right - windowRect.left;

        int[] location = new int[2];
        mAnchorView.getLocationInWindow(location);

        if (mode == DropDown.Mode.Over) {
            int maxHeightAbove = location[1] - windowRect.top - marginHalf * 2;
            int maxItemsAbove = maxHeightAbove / itemHeight;
            int maxHeightBelow = hWindow - location[1] - marginHalf * 2;
            int maxItemsBelow = maxHeightBelow / itemHeight;

            int itemsBelow = Math.min(adapter.getItemCount() - selectedItem, maxItemsBelow);
            int itemsAbove = Math.min(selectedItem, maxItemsAbove);

            int popupX = location[0] - margin - marginHalf;
            int popupY = location[1] - marginHalf * 2 - itemsAbove * itemHeight - (itemHeight - (mAnchorView.getHeight() - mAnchorView.getPaddingTop() -
                    mAnchorView.getPaddingBottom())) / 2 + mAnchorView.getPaddingTop();
            int popupWidth = mAnchorView.getWidth() + margin * 2 + marginHalf * 2 - mAnchorView.getPaddingLeft() - mAnchorView.getPaddingRight();
            int popupHeight = marginHalf * 4 + Math.max(1, itemsAbove + itemsBelow) * itemHeight;

            popupWidth = Math.min(popupWidth, wWindow - marginHalf * 2);
            popupX = Math.max(popupX, 0);
            popupX = Math.min(popupX, wWindow - popupWidth);

            LinearLayoutManager manager = (LinearLayoutManager) recycler.getLayoutManager();
            manager.scrollToPositionWithOffset(selectedItem - itemsAbove, 0);

            update(popupX, popupY, popupWidth, popupHeight);
        } else {
            // not interesting
        }

        super.update();
    }
}

代码太长了,无法在此处粘贴所有详细信息,因此我将为您提供指向该类的链接:DropDownMenu。根据需要使用它。我希望你能找到有用的代码。

我已从指南中为该图像制作了样本。它可以在指南中的示例应用程序中找到 - >菜单/行为

enter image description here enter image description here