Activity is visually divided into two parts, namely 2 layout: a left area1, right area2. At the start of activation, numbers are placed in area1 (from zero to 9). The user can click on the number and drag it to area2. When this figure is removed from area1. How to fix the code so that the user drags the digit from the field1 to the area2, and at the same time this digit from the field1 is not deleted?
public class Main2Activity extends AppCompatActivity implements View.OnTouchListener{ LinearLayout area1, area2; TypedArray arrayResources; LinearLayout.LayoutParams lParams; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); area1 = (LinearLayout) findViewById(R.id.area1); area2 = (LinearLayout) findViewById(R.id.area2); arrayResources = getResources().obtainTypedArray( R.array.resicon); for (int i = 0; i < arrayResources.length(); i++) { TextView textView = new TextView(this); textView.setText(arrayResources.getText(i)); LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); int textViewGravity = Gravity.CENTER_HORIZONTAL; lParams.gravity = textViewGravity; lParams.setMargins(10,5,5,5); textView.setTextColor(Color.WHITE); textView.setTextSize(getResources().getDimension(R.dimen.size1)); textView.setGravity(Gravity.CENTER); textView.setOnTouchListener(this); area1.addView(textView, lParams); } arrayResources.recycle(); area1.setOnDragListener(myOnDragListener); area2.setOnDragListener(myOnDragListener); } OnDragListener myOnDragListener = new OnDragListener() { @Override public boolean onDrag(View v, DragEvent event) { switch (event.getAction()) { case DragEvent.ACTION_DROP: View view = (View)event.getLocalState(); LinearLayout oldParent = (LinearLayout)view.getParent(); oldParent.removeView(view); LinearLayout newParent = (LinearLayout) v; newParent.addView(view); break; } return true; } }; @Override public boolean onTouch(View v, MotionEvent event) { ClipData data = ClipData.newPlainText("", ""); DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); v.startDrag(data, shadowBuilder, v, 0); return true; } } And one more request, could you explain the logic of this record:
View view = (View)event.getLocalState(); LinearLayout oldParent = (LinearLayout)view.getParent(); oldParent.removeView(view); LinearLayout newParent = (LinearLayout) v; newParent.addView(view); In this case, we create a variable of type View, but this is why such a record is:
View view = (View)event.getLocalState(); What value does this variable get in this case?