ViewをD&Dする処理を書く前段階として、setTranslationの使い方を確かめておこうということで、下記のようなコードを書きました。
自分のイメージだと、TextViewをタップするごとに下がっていく感じになる予定だったのですが、1回目のタップでカクっと下に下がったっきり、何度タップしても動いてくれません。
Logを見る限り、onTouchメソッドはタップ毎に実行されています。
なにか足りないんでしょうか?
Java
1import android.graphics.Color; 2import android.support.v7.app.AppCompatActivity; 3import android.os.Bundle; 4import android.view.MotionEvent; 5import android.view.View; 6import android.widget.TextView; 7 8public class MainActivity extends AppCompatActivity implements View.OnTouchListener{ 9 TextView textView; 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 textView = (TextView)findViewById(R.id.textView); 16 textView.setBackgroundColor(Color.rgb(33,132,255)); 17 textView.setOnTouchListener(this); 18 19 20 } 21 22 @Override 23 public boolean onTouch(View v, MotionEvent event) { 24 25 v.setTranslationY(100); 26 Log.d("onTouch","Hello!"); 27 return true; 28 } 29} 30
xml
1<?xml version="1.0" encoding="utf-8"?> 2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="***.danddtest.MainActivity" 11 android:id="@+id/frameLayout"> 12 13 <TextView 14 android:layout_width="300dp" 15 android:layout_height="200dp" 16 android:text="D&D me!" 17 android:layout_gravity="center" 18 android:gravity="center" 19 android:id="@+id/textView"/> 20 21 22</FrameLayout> 23 24``` 25コード 26```
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/06 00:28