IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

ドラックアンドドロップ(Drag and Drop)

ドラックアンドドロップのサンプル

f:id:naotoogawa:20151128133124p:plain

package com.example.dnd02;

import android.content.ClipData;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    private static final class MyTouchListener implements View.OnLongClickListener {

        @Override
        public boolean onLongClick(View view) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.setBackgroundColor(Color.BLACK);
            ((TextView)view).setTextColor(Color.WHITE);
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        }
    }

    private static final class MyDragListener implements View.OnDragListener {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    v.setBackgroundColor(Color.RED);
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    v.setBackgroundColor(Color.GREEN);
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    v.setBackgroundColor(Color.BLUE);
                    break;
                case DragEvent.ACTION_DROP:
                    v.setBackgroundColor(Color.BLACK);

                    EditText view = (EditText) event.getLocalState();
                    view.setBackgroundColor(Color.WHITE);
                    view.setTextColor(Color.BLACK);
                    ((TextView)v).setText(view.getText());

                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    v.setBackgroundColor(Color.WHITE);
                default:
                    break;
            }
            return true;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getTextView(R.id.dragItem).setOnLongClickListener(new MyTouchListener());
        getTextView(R.id.dropItem).setOnDragListener(new MyDragListener());
    }


    private TextView getTextView(int id) {
        return (TextView)this.findViewById(id);
    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.dnd02.MainActivity"
    >

    <EditText android:text="dragItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dragItem"
        android:textSize="35dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dropItem"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="here"
        android:textSize="35dp" />

</RelativeLayout>