拖动后动态添加的视图在relativelayout中不会相互重叠

时间:2016-06-14 12:01:06

标签: android drag-and-drop relativelayout

我有一个包含按钮的relativelayout

我的布局

import random
sd = random.choice([1,2,3,4])
ed = random.choice([1,2,3,4]) + sd
del sd
del ed

# The delay is still constant for the entire series and not random per row
s = pd.Series(True, pd.bdate_range("20100101", periods=100, freq="MS") + pd.DateOffset(14 + sd))
e = pd.Series(True, pd.bdate_range("20100101", periods=100, freq="MS") + pd.DateOffset(19 + ed))

此按钮可添加单击时拖放的文本视图

我的问题是如何防止这些文本在彼此拖放时相互重叠

动态添加和拖动textviews我说的看起来像this

我的主要活动

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

<Button
    android:layout_height="wrap_content"
    android:text="T"
    android:layout_width="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:id="@+id/mainButton1"/>

<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_above="@id/xvalue"
    android:background="#E1A29D"
    android:id="@+id/edtxtlayout"/>

** MyLongClickListner **

public class MainActivity extends Activity 
{

RelativeLayout dropLayoutNew;

Button addText;

@Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    dropLayoutNew = (RelativeLayout) findViewById(R.id.edtxtlayout);    
    dropLayoutNew.setOnDragListener(new MyDragListener());
    addText = (Button) findViewById(R.id.mainButton1);
    addText.setOnClickListener(new MyButtonClickListener());





}

}

我的clicklistener

public class MyLongClickListner implements OnLongClickListener
 {

@Override
public boolean onLongClick(View v)
{

    ClipData dragdata = ClipData.newPlainText("","");

    View.DragShadowBuilder shdwbldr = new View.DragShadowBuilder(v);

    v.startDrag(dragdata, shdwbldr, v, 0);
    v.setVisibility(View.INVISIBLE);

    return true;
}

   }

我的拖拉机

public class MyButtonClickListener implements OnClickListener
 {

@Override
public void onClick(View v)
{
    ViewGroup relativeParent = (ViewGroup) v.getParent();
    CustomEdittext edttxt = new CustomEdittext(v.getContext());
    relativeParent.addView(edttxt);
    edttxt.setOnLongClickListener(new MyLongClickListener());

}

  }

myButtonClickListener

public class MyDragListener implements OnDragListener
 {

private LayoutParams params;

@Override
public boolean onDrag(View v, DragEvent event)
{
    View view = (View) event.getLocalState();


    switch(event.getAction())
    {
        case DragEvent.ACTION_DRAG_STARTED:

            params = (RelativeLayout.LayoutParams) view.getLayoutParams();  
            break;

        case DragEvent.ACTION_DRAG_ENTERED:
            int x = (int) event.getX();
            int y = (int) event.getY();

            break;

        case DragEvent.ACTION_DRAG_EXITED :

            break;

        case DragEvent.ACTION_DRAG_LOCATION  :
            x=  (int) event.getX();
            y =  (int) event.getY();
            break;

        case DragEvent.ACTION_DRAG_ENDED   :

            break;

        case DragEvent.ACTION_DROP:

            x = (int) event.getX();
            y = (int) event.getY();
            params.leftMargin = x;
            params.topMargin = y;

            view.setLayoutParams(params);
            view.setVisibility(View.VISIBLE);

            break;
        default: break;
    }
    return true;
}


 }

0 个答案:

没有答案