Android API 7 ++的拖放功能

时间:2011-12-20 16:23:52

标签: android

我必须使用一个为API 7 ++创建拖放功能的人的公共库,因为在Android中它是从API 11开始正式实现的。

我不知道如何正确使用它。

我希望我将Button1拖到Button2上。 Button2现在应该有Button1的文本。 但是,如果我再次将Button2拖到示例Button3,则应显示原始的Button2文本。 我已经管理过Button 2有Button1的文本,但是进一步对我来说是一个问题.. 我不知道如何实现这一目标......

以下是一些代码:

public void setDragArea(final DragArea dragArea, final TextView reportView){
        this.setOnTouchListener(new View.OnTouchListener(){
          public boolean onTouch(View view, MotionEvent event){
            if (event.getAction() == MotionEvent.ACTION_DOWN) { 
              Bundle data = new Bundle();
              data.putCharSequence("buttonText", getText());
              dragArea.startDrag(data, new DrawableDragShadowBuilder(DraggableDot.this, mTranslucentDot, new Point((int)event.getX() - getPaddingLeft(), (int)event.getY() - getPaddingTop())));
              return true;
            } else {
              return false;
            }
          }
        });

        dragArea.addDragListener(DraggableDot.this, new OnDragListener(){
          public void onDrag(View view, DragEvent dragEvent){
              final Bundle data = dragEvent.getBundle();
            switch (dragEvent.getAction()){
              case DragEvent.ACTION_DRAG_STARTED:
                DraggableDot.this.setBackgroundDrawable(mGreenDot);
                break;
              case DragEvent.ACTION_DRAG_ENTERED:               
                DraggableDot.this.setBackgroundDrawable(mWhiteDot);
                break;
              case DragEvent.ACTION_DRAG_EXITED:
                DraggableDot.this.setBackgroundDrawable(mGreenDot);
                break;
              case DragEvent.ACTION_DROP:               
                final CharSequence dropText = data.getCharSequence("buttonText");           

                DraggableDot.this.setText(dropText);
                DraggableDot.this.setBackgroundDrawable(mRedDot);
                break;
              case DragEvent.ACTION_DRAG_ENDED:
                DraggableDot.this.setBackgroundDrawable(mRedDot);
                break;
              default:
                break;
            }
          }

        });
      }

Drag.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<com.lernapp.src.DragArea
    android:id="@+id/drag_area"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >
    <RelativeLayout
        android:id="@+id/dots"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <com.lernapp.src.DraggableDot
            android:id="@+id/dot_one"
            android:text="Ich bin Button 1 Text"
            android:background="@+drawable/red_dot"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            />
        <com.lernapp.src.DraggableDot
            android:id="@+id/dot_two"
            android:text="Ich bin Button 2 Text"
            android:background="@drawable/red_dot"
            android:textStyle="bold"
            android:layout_below="@id/dot_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            />
        <com.lernapp.src.DraggableDot
            android:id="@+id/dot_three"
            android:text="3"
            android:background="@+drawable/white_dot"
            android:textStyle="bold"
            android:layout_toRightOf="@id/dot_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            />
        <com.lernapp.src.DraggableDot
            android:id="@+id/dot_four"
            android:text="4"
            android:background="@+drawable/white_dot"
            android:textStyle="bold"
            android:layout_below="@id/dot_three"
            android:layout_toRightOf="@id/dot_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            />
    </RelativeLayout>
</com.lernapp.src.DragArea>
<TextView
    android:id="@+id/report_view"
    android:text="1 + 1 = 2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:gravity="center"
    android:padding="10dp"
    style="@android:style/TextAppearance.Large"
    android:textStyle="bold"
    android:shadowColor="#BB000000"
    android:shadowRadius="2.75"
    android:background="@android:drawable/title_bar"
    />
</LinearLayout>

0 个答案:

没有答案
相关问题