Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

android - Draggable Button getting out of RelativeLayout whenever dragged

I am developing an application in which I am using Drag and Drop functionality. In that I am generating buttons OnClick.The code is working fine but when I drag the button to the corners of the relativelayout,button gets out of the layout. I want it to stay inside of the layout.

Before Dragging the button

1st

After Dragging the button (on top corner in this example)

enter image description here

As you can see that the button is getting out of the layout whenever I drag it towards the end/corner of the layout, I want it to stay in the layout intact, display full button. How can I do that?

Thanks in advance.!

MainActivity.java

public class MainActivity extends Activity implements OnTouchListener {
    Button btnAddButton;
    RelativeLayout rl1;
    int i = 1;
    private int _xDelta;
    private int _yDelta;
    ViewGroup _root;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnAddButton = (Button) findViewById(R.id.btnAdd);

        rl1 = (RelativeLayout) findViewById(R.id.relative_layout);
        _root = (ViewGroup)findViewById(R.id.relative_layout);
        btnAddButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                add(v);

            }
        });
    }
    public void add(View v) {
        Button btn = new Button(MainActivity.this);
        //btn.setId(i);
        RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

        int a=(int) (Math.random()*100);
       // Toast.makeText(MainActivity.this, String.valueOf(Math.random()*100), 1).show();//double a=Math.random();
        layoutParam.leftMargin = 30+a;

        if (i > 1) {

            layoutParam.addRule(RelativeLayout.BELOW, (i - 1));
        }

        btn.setText("Button" + i);
        rl1.addView(btn, layoutParam);
        btn.setOnTouchListener(this);
        i++;
    } 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
         final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    _xDelta = X - lParams.leftMargin;
                    _yDelta = Y - lParams.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 layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    layoutParams.leftMargin = X - _xDelta;
                    layoutParams.topMargin = Y - _yDelta;
                    layoutParams.rightMargin = -250;
                    layoutParams.bottomMargin = -250;
                    v.setLayoutParams(layoutParams);
                    break;
            }
            _root.invalidate();
        return true;
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="AddButton"
        android:text="Button" />

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btnAdd" >
    </RelativeLayout>

</RelativeLayout>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was able to tackle this by using these conditions:

        if ((v.getY() < lay.getY())) {

            v.setX(xVal);
            v.setY(yVal);
        } else if (v.getX() < lay.getX()) {

            v.setX(xVal);
            v.setY(yVal);
        } else if (v.getX() + v.getWidth() > lay.getX() + lay.getWidth()) {

            v.setX(xVal);
            v.setY(yVal);
        } else if (v.getY() + v.getHeight() > lay.getY() + lay.getHeight()) {

            v.setX(xVal);
            v.setY(yVal);
        } else {
            for (int i = 0; i <= viewIdList.size() - 1; i++) {
                if (v.getId() != viewIdList.get(i).getId()) {

                    View v3 = viewIdList.get(i);
                    Rect rect1 = new Rect(v.getLeft(), v.getTop(),
                            v.getRight(), v.getBottom());
                    v.getHitRect(rect1);
                    Rect rect2 = new Rect(v3.getLeft(), v3.getTop(),
                            v3.getRight(), v3.getBottom());
                    v3.getHitRect(rect2);
                    if (Rect.intersects(rect1, rect2)) {

                        System.out.println("overlap");
                        v.setX(xVal);
                        v.setY(yVal);
                    }

where v is the view (ImageView in my case) and this code is written in MotionEvent.ACTION_UP of onTouchListener.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...