滚动时使视图坚持使用RecyclerView项目

时间:2016-03-08 10:27:31

标签: android android-recyclerview

以下是我要做的事情:我在屏幕底部有一个水平的RecyclerView。当我点击某个项目时,我想在该特定项目上方显示一个弧形菜单(类似于此http://www.andbrain.com/wp-content/uploads/2015/04/device-2015-04-07-165251.png),它允许一些操作:

enter image description here

由于弧形菜单不能包含在项目视图中,我使用了由项目视图管理的PopupWindow,但是当我在RecyclerView上滚动时,我正在努力解决位置问题。

public class ItemView extends RelativeLayout implements View.OnLongClickListener {

    @Bind(R.id.avatar)
    TextView mAvatar;

    private PopupWindow mPopupWindow;

    public ItemView(Context context) {
        super(context);
        setOnLongClickListener(this);
    }

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnLongClickListener(this);
    }

    public ItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnLongClickListener(this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.bind(this);
    }

    private void initPopup() {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View popupView = layoutInflater.inflate(R.layout.menu_arc, null);
        mPopupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mPopupWindow.setClippingEnabled(false);
        popupView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
            }
        });
    }

    @Override
    public boolean onLongClick(View v) {
        if (mPopupWindow == null) {
            initPopup();
        }

        int avatarPos[] = new int[2];
        mAvatar.getLocationOnScreen(avatarPos);

        Rect avatarRect = new Rect(avatarPos[0], avatarPos[1], avatarPos[0]
                + mAvatar.getWidth(), avatarPos[1] + mAvatar.getHeight());

        mPopupWindow.getContentView().measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        int contentViewHeight = mPopupWindow.getContentView().getMeasuredHeight();
        int contentViewWidth = mPopupWindow.getContentView().getMeasuredWidth();

        int positionX = avatarRect.centerX() - (contentViewWidth / 2);
        int positionY = avatarRect.centerY() - contentViewHeight;

        mPopupWindow.showAtLocation(getRootView(), Gravity.NO_GRAVITY, positionX, positionY);

        return true;
    }

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

    public void updateMenuPosition() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) { // mPopupWindow.isShowing() is always false, why ?
            int avatarPos[] = new int[2];
            mAvatar.getLocationOnScreen(avatarPos);

            Rect avatarRect = new Rect(avatarPos[0], avatarPos[1], avatarPos[0]
                    + mAvatar.getWidth(), avatarPos[1] + mAvatar.getHeight());

            int contentViewHeight = mPopupWindow.getContentView().getMeasuredHeight();
            int contentViewWidth = mPopupWindow.getContentView().getMeasuredWidth();

            int positionX = avatarRect.centerX() - (contentViewWidth / 2);
            int positionY = avatarRect.centerY() - contentViewHeight;
            mPopupWindow.update(positionX, positionY, contentViewWidth, contentViewHeight);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如何在activity_main.xml中使用android.support.design.widget.FloatingActionButton