美文网首页
动态添加view

动态添加view

作者: 龙猫跑跑跑 | 来源:发表于2016-05-13 13:38 被阅读55次

1.解决在次动态添加view后,已添加view位置变动

http://www.programgo.com/article/37723079049/

/** * 界面添加标签 * * @param selectTag */private void insertTag(final String selectTag) {    /*    添加标签,并布局     */    final TagBtn tagBtn = new TagBtn(this, deviceScreenWidth, deviceScreenWidth);    tagBtn.setText(selectTag);    tagBtn.setTag(selectTag);    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);    rlp.addRule(RelativeLayout.CENTER_IN_PARENT);    tagBtn.setLayoutParams(rlp);    tagContainer.addView(tagBtn);    mTagBtnList.put(selectTag, tagBtn);    /*    标签的信息内容     */    final LabelBean labelBean = new LabelBean();    labelBean.setValue(selectTag);    labelBean.setType("1");    tagBtn.setOnTouchListener(new View.OnTouchListener() {        @Override        public boolean onTouch(View v, MotionEvent event) {            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);            Runnable mLongPressed = new Runnable() {                @Override                public void run() {                    mDeleteTagIv.setVisibility(View.VISIBLE);                    tag = selectTag;                    UtilLog.d(TAG, "选中的标签:" + tagBtn.getTag());                    UtilLog.d(TAG, "选中的标签:" + tag);                }            };            if (event.getAction() == MotionEvent.ACTION_DOWN) {                mHandler.postDelayed(mLongPressed, 1000);            }            switch (event.getAction()) {                case MotionEvent.ACTION_DOWN:                    lastX = event.getRawX();                    lastY = event.getRawY();                    break;                case MotionEvent.ACTION_MOVE:                    int dx = (int) (event.getRawX() - lastX);                    int dy = (int) (event.getRawY() - lastY);                    if (dx > 100 && dy > 100) {                        mHandler.removeCallbacks(mLongPressed);                    }                                        left = tagBtn.getLeft() + dx;                    top = tagBtn.getTop() + dy;                    right = tagBtn.getRight() + dx;                    bottom = tagBtn.getBottom() + dy;                    if (left < 0) {                        left = 0;                        right = left + tagBtn.getWidth();                    }                    if (right > deviceScreenWidth) {                        right = deviceScreenWidth;                        left = right - tagBtn.getWidth();                    }                    if (top < 0) {                        top = 0;                        bottom = top + tagBtn.getHeight();                    }                    if (bottom > deviceScreenWidth) {                        bottom = deviceScreenWidth;                        top = bottom - tagBtn.getHeight();                    }                                        rlp.setMargins(left, top, right, bottom);                    tagBtn.setLayoutParams(rlp);                    lastX = (int) event.getRawX();                    lastY = (int) event.getRawY();                    break;                case MotionEvent.ACTION_UP:                    break;                default:                    break;            }            return true;        }    });    mLabelBeenList.add(labelBean);

相关文章

网友评论

      本文标题:动态添加view

      本文链接:https://www.haomeiwen.com/subject/ykxmrttx.html