如何冻结相对布局上新添加的视图及其参数?

时间:2018-08-05 07:00:34

标签: android arraylist onclicklistener ontouchlistener android-relativelayout

我正在尝试为一家餐厅设计一个应用程序,我想模拟他的建筑物,该建筑物包含6层,每层都有一个放置在相对布局(地面)上的不同表格(ImageViews)的列表,现在添加表格后由用户将它们拖动到各自的位置,我想冻结它们,这样当我单击一楼的按钮并回到主楼层时,我会看到它们的位置和大小! 我曾经将数据存储在ArrayList中,所以在将新表添加到相对布局中时,我将新表的高度,宽度,左边界,topMargin和表号存储在数组列表中,并且可以工作,但是我的问题是以后如何使用它们来检索它们它们的onClick和onTouch并对其进行控制(我的意思是拖放后必须在数组中改变位置),如果可能的话请告诉我。 查看屏幕截图:screenshoot

这是我的简单代码:

addSquaredTableButton.setOnClickListener(onAddTableClickListener);

OnClickListener onAddTableClickListener = new OnClickListener() {
    @Override
    public void onClick(View view) {
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(150, 120);
                ImageView newTable = new ImageView(DineIn.this);
                newTable.setLayoutParams(params);
                newTable.setBackgroundResource(R.drawable.circled_table_with_6);

                RelativeLayout.LayoutParams linearParams = new RelativeLayout.LayoutParams(150, 120);
                linearParams.leftMargin = 300;
                linearParams.topMargin = 100;
                LinearLayout newLinear = new LinearLayout(DineIn.this);
                newLinear.setLayoutParams(linearParams);

                newLinear.setOnTouchListener(new onTouchListener());
                newLinear.addView(newTable);
                land.addView(newLinear);

                Tables table = new Tables(150 , 110 , R.drawable.circled_table_with_6 , 300 , 100 , land.getChildCount()-2);
                listMainFloor.add(table);

    }

};

private final class onTouchListener implements OnTouchListener {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        if (gestureDetector.onTouchEvent(motionEvent)) { // onClick
            if (!clicked) {
                focused = (LinearLayout) view;
                view.setBackgroundResource(R.drawable.focused_table);
                slideUp(edit);
                clicked = true;
            } else {
                view.setBackgroundDrawable(null);
                slideDown(edit);
                clicked = false;
            }


            return true;
        } else {

            final int x = (int) motionEvent.getRawX();
            final int y = (int) motionEvent.getRawY();


            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    xDelta = x - params.leftMargin;
                    yDelta = y - params.topMargin;
                    break;

                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;

                case MotionEvent.ACTION_MOVE:

                    RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    params2.leftMargin = x - xDelta;
                    params2.topMargin = y - yDelta;
                    view.setLayoutParams(params2);

                    break;
            }
            land.invalidate();
            return true;
        }
    }
}

0 个答案:

没有答案