Android拖放实施

时间:2016-08-17 21:02:30

标签: java android xml

我是Android的初学者,我正在尝试制作一个简单的单词争夺游戏。目前,我只想弄清楚如何使用拖放功能。我经历了一些教程,并尝试应用我学到的东西,但是当我尝试将其中一个字母拼贴放入其目标时,我的程序不断崩溃。

我一直遇到的运行时错误是

java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.LinearLayout

它说这个错误发生在java代码的这一行

LinearLayout newParent = (LinearLayout)v;

但是我在教程中的那一行工作得很好。显然我做错了什么。我从昨天起就一直坚持这一点,我真的很感激一些帮助。

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="41dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1"
android:paddingLeft="0dp"
android:paddingRight="10dp">


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="0:59"
    android:id="@+id/textViewTimer"
    android:editable="false"
    android:gravity="center_horizontal"
    android:textSize="50dp"
    android:textStyle="bold" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:paddingTop="60dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target1"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target2"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target3"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target4"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target5"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="156dp"
    android:paddingBottom="0dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter1"
        android:src="@drawable/letter_a"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter2"
        android:src="@drawable/letter_g"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter3"
        android:src="@drawable/letter_r"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter4"
        android:src="@drawable/letter_e"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter5"
        android:src="@drawable/letter_v"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
</LinearLayout>
</LinearLayout>

这是我的java代码

public class MainActivity extends Activity {

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

    //set OnTouchListeners for the letters
    findViewById(R.id.imageView_letter1).setOnTouchListener(new     MyTouchListener());
    findViewById(R.id.imageView_letter2).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imageView_letter3).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imageView_letter4).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imageView_letter5).setOnTouchListener(new MyTouchListener());

    //set OnDragListeners for the drop targets
    findViewById(R.id.imageView_target1).setOnDragListener(new MyDragListener());
    findViewById(R.id.imageView_target2).setOnDragListener(new MyDragListener());
    findViewById(R.id.imageView_target3).setOnDragListener(new MyDragListener());
    findViewById(R.id.imageView_target4).setOnDragListener(new MyDragListener());
    findViewById(R.id.imageView_target5).setOnDragListener(new MyDragListener());
}

private final class MyTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("","");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        }
        else{
            return false;
        }
    }
}

class MyDragListener implements View.OnDragListener {
    Drawable emptySpace = getResources().getDrawable(R.drawable.blank_space);

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                //do nothing
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
             //   //could highlight drop target box
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                //un-highlight
                break;
            case DragEvent.ACTION_DROP:
                //dropped
                View view = (View)event.getLocalState();
                LinearLayout oldParent = (LinearLayout)view.getParent();
                oldParent.removeView(view);
                LinearLayout newParent = (LinearLayout)v;
                newParent.addView(view);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                v.setBackgroundDrawable(emptySpace);
                break;
            default:
                break;
        }
        return true;
    }
}

}

1 个答案:

答案 0 :(得分:1)

您正在setOnDragListener()小部件上调用ImageView。因此,传递到View的{​​{1}}将是其中一个onDrag()小部件,而不是ImageView