質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

1回答

1735閲覧

DragEvent.ACTION_DROPが呼ばれない

akamakku

総合スコア191

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2016/10/24 05:40

D&Dの処理を勉強しようと思い、簡単なサンプルを作っています。

  1. textViewをLongClickすることででstartDragを呼ぶ。
  2. GridLayoutに配置したtextView1~9のどこでDropしたのかをToastで表示する。

だけのアプリです。

DragされるTextViewには、OnLongClickListenerをsetしていますし、
Drop相手のTextViewには、OnDragListenerをsetしています。

それでも、OnDragListener内のOnDragメソッドで受け取るのは、ACTION_DRAG_STARTEDとACTION_DRAG_ENDEDのみで、ACTION_DROPを受け取りません。
何が悪いんでしょうか?

よろしくお願いします。

Java

1public class MainActivity extends AppCompatActivity{ 2 TextView textView; 3 TextView[] textViews = new TextView[9]; 4 int[] textViewIds = {R.id.textView1, 5 R.id.textView2, 6 R.id.textView3, 7 R.id.textView4, 8 R.id.textView5, 9 R.id.textView6, 10 R.id.textView7, 11 R.id.textView8, 12 R.id.textView9}; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 19 textView = (TextView) findViewById(R.id.textView); 20 textView.setOnLongClickListener(new OnGridLongClickListener()); 21 22 for(int i = 0; i < 9; i++){ 23 textViews[i] = (TextView)findViewById(textViewIds[i]); 24 textViews[i].setOnDragListener(new OnGridDragListener(i+1)); 25 } 26 27 textView.setBackgroundColor(Color.rgb(33, 132, 255)); 28 29 } 30 31}

Java

1public class OnGridDragListener implements View.OnDragListener { 2 3 int index; 4 5 OnGridDragListener(int i){ 6 super(); 7 index = i; 8 } 9 @Override 10 public boolean onDrag(View v, DragEvent event) { 11 Log.d("onDrag"+index,""+event.getAction()); 12 switch (event.getAction()){ 13 case DragEvent.ACTION_DROP: 14 Toast.makeText(v.getContext(),"You dropped the view in textView"+index,Toast.LENGTH_SHORT); 15 Log.d("onDrag","You dropped the view in textView"+index); 16 return true; 17 default: 18 } 19 return false; 20 } 21 22}

Java

1public class OnGridLongClickListener implements View.OnLongClickListener { 2 @Override 3 public boolean onLongClick(View v) { 4 v.startDrag(null, new View.DragShadowBuilder(v), v, 0); 5 return true; 6 } 7}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 android:paddingBottom="@dimen/activity_vertical_margin" 10 android:paddingLeft="@dimen/activity_horizontal_margin" 11 android:paddingRight="@dimen/activity_horizontal_margin" 12 android:paddingTop="@dimen/activity_vertical_margin" 13 tools:context="example.danddtest.MainActivity" 14 android:id="@+id/linearLayout"> 15 16 <TextView 17 android:text="D&amp;D me!" 18 android:layout_gravity="center" 19 android:gravity="center" 20 android:layout_weight="1" 21 android:background="#3e64b6" 22 android:id="@+id/textView" 23 android:layout_width="150dp" 24 android:layout_height="wrap_content" /> 25 26 <android.support.v7.widget.GridLayout 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:layout_weight="2" 30 app:columnCount="3" 31 app:rowCount="3" 32 android:id="@+id/gridLayout"> 33 34 <TextView 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 app:layout_rowWeight="1" 38 app:layout_columnWeight="1" 39 app:layout_column="0" 40 app:layout_row="0" 41 android:text="1" 42 app:layout_gravity="fill" 43 android:gravity="center" 44 android:background="#aaff0044" 45 android:layout_margin="5dp" 46 android:id="@+id/textView1"/> 47 <TextView 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 app:layout_rowWeight="1" 51 app:layout_columnWeight="1" 52 app:layout_column="1" 53 app:layout_row="0" 54 android:text="2" 55 app:layout_gravity="fill" 56 android:gravity="center" 57 android:background="#aaff0044" 58 android:layout_margin="5dp" 59 android:id="@+id/textView2"/> 60 <TextView 61 android:layout_width="wrap_content" 62 android:layout_height="wrap_content" 63 app:layout_rowWeight="1" 64 app:layout_columnWeight="1" 65 app:layout_column="2" 66 app:layout_row="0" 67 android:text="3" 68 app:layout_gravity="fill" 69 android:gravity="center" 70 android:background="#aaff0044" 71 android:layout_margin="5dp" 72 android:id="@+id/textView3"/> 73 <TextView 74 android:layout_width="wrap_content" 75 android:layout_height="wrap_content" 76 app:layout_rowWeight="1" 77 app:layout_columnWeight="1" 78 app:layout_column="0" 79 app:layout_row="1" 80 android:text="4" 81 app:layout_gravity="fill" 82 android:gravity="center" 83 android:background="#aaff0044" 84 android:layout_margin="5dp" 85 android:id="@+id/textView4"/> 86 <TextView 87 android:layout_width="wrap_content" 88 android:layout_height="wrap_content" 89 app:layout_rowWeight="1" 90 app:layout_columnWeight="1" 91 app:layout_column="1" 92 app:layout_row="1" 93 android:text="5" 94 app:layout_gravity="fill" 95 android:gravity="center" 96 android:background="#aaff0044" 97 android:layout_margin="5dp" 98 android:id="@+id/textView5"/> 99 <TextView 100 android:layout_width="wrap_content" 101 android:layout_height="wrap_content" 102 app:layout_rowWeight="1" 103 app:layout_columnWeight="1" 104 app:layout_column="2" 105 app:layout_row="1" 106 android:text="6" 107 app:layout_gravity="fill" 108 android:gravity="center" 109 android:background="#aaff0044" 110 android:layout_margin="5dp" 111 android:id="@+id/textView6"/> 112 <TextView 113 android:layout_width="wrap_content" 114 android:layout_height="wrap_content" 115 app:layout_rowWeight="1" 116 app:layout_columnWeight="1" 117 app:layout_column="0" 118 app:layout_row="2" 119 android:text="7" 120 app:layout_gravity="fill" 121 android:gravity="center" 122 android:background="#aaff0044" 123 android:layout_margin="5dp" 124 android:id="@+id/textView7"/> 125 <TextView 126 android:layout_width="wrap_content" 127 android:layout_height="wrap_content" 128 app:layout_rowWeight="1" 129 app:layout_columnWeight="1" 130 app:layout_column="1" 131 app:layout_row="2" 132 android:text="8" 133 app:layout_gravity="fill" 134 android:gravity="center" 135 android:background="#aaff0044" 136 android:layout_margin="5dp" 137 android:id="@+id/textView8"/> 138 <TextView 139 android:layout_width="wrap_content" 140 android:layout_height="wrap_content" 141 app:layout_rowWeight="1" 142 app:layout_columnWeight="1" 143 app:layout_column="2" 144 app:layout_row="2" 145 android:text="9" 146 app:layout_gravity="fill" 147 android:gravity="center" 148 android:background="#aaff0044" 149 android:layout_margin="5dp" 150 android:id="@+id/textView9"/> 151 152 </android.support.v7.widget.GridLayout> 153 154</LinearLayout> 155

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

少し調べてみましたが、
ACTION_DRAG_STARTEDでtrueを返しておかないと、
ACTION_DRAG_ENDEDしか呼ばれなくなるみたいですね。

情報ソースとしては、古い記事ですが
TechBooster - Drag and Dropを用いてViewをドラッグする
と、
Android Developers - Drag and Drop
Responding to a drag start .2の部分に書いてあります。(あるような気がします。)

投稿2016/10/24 06:25

abs123

総合スコア1280

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

akamakku

2016/10/24 06:36

そうなんですね!!!!!! Android Developers のドキュメントはサラッとは呼んでみたつもりだったのですが、サラッとではだめだったみたいです。。。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問