Я пытаюсь создать карточную игру с функцией перетаскивания. Есть ли способ использовать функцию перетаскивания в моем основном действии, не запуская функцию, объявляющую ее в MainActivity? Это дает мне ошибку.
Вот мой код MainActivity:
package com.techietoystore.newsnakes;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Application;
import android.content.DialogInterface;
import android.media.Image;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;
//import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
//import android.widget.TextView;
//import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ImageView iv_1, iv_2, iv_3, iv_4, iv_5, iv_6, iv_7, iv_8;
private Button button;
private iv_1 imageView1;
ArrayList<Integer> cards;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_1 = (ImageView) findViewById(R.id.imageView1);
iv_2 = (ImageView) findViewById(R.id.imageView2);
iv_3 = (ImageView) findViewById(R.id.imageView3);
iv_4 = (ImageView) findViewById(R.id.imageView4);
iv_5 = (ImageView) findViewById(R.id.imageView5);
iv_6 = (ImageView) findViewById(R.id.imageView6);
iv_7 = (ImageView) findViewById(R.id.imageView7);
iv_8 = (ImageView) findViewById(R.id.imageView8);
iv_1.setVisibility(View.INVISIBLE);
iv_2.setVisibility(View.INVISIBLE);
iv_3.setVisibility(View.INVISIBLE);
iv_4.setVisibility(View.INVISIBLE);
iv_5.setVisibility(View.INVISIBLE);
iv_6.setVisibility(View.INVISIBLE);
iv_7.setVisibility(View.INVISIBLE);
iv_8.setVisibility(View.INVISIBLE);
cards = new ArrayList<>();
cards.add(109);
cards.add(110);
cards.add(111);
cards.add(112);
cards.add(113);
cards.add(114);
cards.add(115);
cards.add(116);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Collections.shuffle(cards);
//sort hands
List<Integer> player1Cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
player1Cards.add(cards.get(i));
Collections.sort(player1Cards);
}
List<Integer> player2Cards = new ArrayList<>();
for (int i = 4; i < 8; i++) {
player2Cards.add(cards.get(i));
Collections.sort(player2Cards);
}
//deal
assignImages(player1Cards.get(0), iv_1);
assignImages(player1Cards.get(1), iv_2);
assignImages(player1Cards.get(2), iv_3);
assignImages(player1Cards.get(3), iv_4);
assignImages(player2Cards.get(0), iv_5);
assignImages(player2Cards.get(1), iv_6);
assignImages(player2Cards.get(2), iv_7);
assignImages(player2Cards.get(3), iv_8);
iv_1.setVisibility(View.VISIBLE);
iv_2.setVisibility(View.VISIBLE);
iv_3.setVisibility(View.VISIBLE);
iv_4.setVisibility(View.VISIBLE);
iv_5.setVisibility(View.VISIBLE);
iv_6.setVisibility(View.VISIBLE);
iv_7.setVisibility(View.VISIBLE);
iv_8.setVisibility(View.VISIBLE);
}
public void assignImages(int card, ImageView image) {
switch (card) {
case 109:
image.setImageResource(R.drawable.r13);
break;
case 110:
image.setImageResource(R.drawable.r14);
break;
case 111:
image.setImageResource(R.drawable.r15);
break;
case 112:
image.setImageResource(R.drawable.y11);
break;
case 113:
image.setImageResource(R.drawable.y12);
break;
case 114:
image.setImageResource(R.drawable.y13);
break;
case 115:
image.setImageResource(R.drawable.y14);
break;
case 116:
image.setImageResource(R.drawable.y15);
break;
}
}
});
}
//play card
private class iv_1 {
}
}
Это код для перетаскивания:
import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.techietoystore.newsnakes.R;
public class dragFunction {
public class MainActivity extends AppCompatActivity implements View.OnDragListener, View.OnLongClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Find all views and set Tag to all draggable views
ImageView imgVw = (ImageView) findViewById(R.id.imageView1);
imgVw.setTag("ANDROID ICON");
imgVw.setOnLongClickListener(this);
//Set Drag Event Listeners for defined layouts
// findViewById(R.id.layout1).setOnDragListener(this);
findViewById(R.id.layout2).setOnDragListener(this);
// findViewById(R.id.layout3).setOnDragListener(this);
}
@Override
public boolean onLongClick(View v) {
// Create a new ClipData.Item from the ImageView object's tag
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
// Create a new ClipData using the tag as a label, the plain text MIME type, and
// the already-created item. This will create a new ClipDescription object within the
// ClipData, and set its MIME type entry to "text/plain"
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item);
// Instantiates the drag shadow builder.
View.DragShadowBuilder dragshadow = new View.DragShadowBuilder(v);
// Starts the drag
v.startDrag(data // data to be dragged
, dragshadow // drag shadow builder
, v // local data about the drag and drop operation
, 0 // flags (not currently used, set to 0)
);
return true;
}
// This is the method that the system calls when it dispatches a drag event to the listener.
@Override
public boolean onDrag(View v, DragEvent event) {
// Defines a variable to store the action type for the incoming event
int action = event.getAction();
// Handles each of the expected events
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
// Determines if this View can accept the dragged data
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
// if you want to apply color when drag started to your view you can uncomment below lines
// to give any color tint to the View to indicate that it can accept data.
// v.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);
// Invalidate the view to force a redraw in the new tint
// v.invalidate();
// returns true to indicate that the View can accept the dragged data.
return true;
}
// Returns false. During the current drag and drop operation, this View will
// not receive events again until ACTION_DRAG_ENDED is sent.
return false;
case DragEvent.ACTION_DRAG_ENTERED:
// Applies a GRAY or any color tint to the View. Return true; the return value is ignored.
v.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
// Invalidate the view to force a redraw in the new tint
v.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore the event
return true;
case DragEvent.ACTION_DRAG_EXITED:
// Re-sets the color tint to blue. Returns true; the return value is ignored.
// view.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);
//It will clear a color filter .
v.getBackground().clearColorFilter();
// Invalidate the view to force a redraw in the new tint
v.invalidate();
return true;
case DragEvent.ACTION_DROP:
// Gets the item containing the dragged data
ClipData.Item item = event.getClipData().getItemAt(0);
// Gets the text data from the item.
String dragData = item.getText().toString();
// Displays a message containing the dragged data.
//Toast.makeText(this, "Dragged data is " + dragData, Toast.LENGTH_SHORT).show();
// Turns off any color tints
v.getBackground().clearColorFilter();
// Invalidates the view to force a redraw
v.invalidate();
View vw = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) vw.getParent();
owner.removeView(vw); //remove the dragged view
//caste the view into LinearLayout as our drag acceptable layout is LinearLayout
LinearLayout container = (LinearLayout) v;
container.addView(vw);//Add the dragged view
vw.setVisibility(View.VISIBLE);//finally set Visibility to VISIBLE
// Returns true. DragEvent.getResult() will return true.
return true;
case DragEvent.ACTION_DRAG_ENDED:
// Turns off any color tinting
v.getBackground().clearColorFilter();
// Invalidates the view to force a redraw
v.invalidate();
// Does a getResult(), and displays what happened.
// An unknown action type was received.
// default:
// Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
// break;
}
return false;
}
}
}
Я также объявил новый класс для перетаскивания и функция падения. Смогу ли я также вызвать класс перетаскивания в моем коде MainActivity?