0
私は右のビューにボックスをドラッグするたびに、私は Androidのドラッグアンドドロップ機能
事前
に感謝を行いますヘルプのいずれかの種類がここにMainActivityの私のソースコードであるアンドロイドアプリケーションで初心者です。 Javaのpackage com.condorpos.demoapp2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnDragListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set ontouch listener for box view
findViewById(R.id.box_view1).setOnTouchListener(this);
findViewById(R.id.box_view2).setOnTouchListener(this);
findViewById(R.id.box_view3).setOnTouchListener(this);
findViewById(R.id.box_view4).setOnTouchListener(this);
//set ondrag listener for right and left parent views
findViewById(R.id.left_view).setOnDragListener(this);
findViewById(R.id.right_view).setOnDragListener(this);
}
@Override
public boolean onDrag(View v, DragEvent event) {
//TODO Auto-generated method stub
if(event.getAction()==DragEvent.ACTION_DROP){
//we want to make sure it is dropped only to left and right parent view
View view = (View)event.getLocalState();
if(v.getId() == R.id.left_view || v.getId() == R.id.right_view){
ViewGroup source = (ViewGroup) view.getParent();
source.removeView(view);
LinearLayout target = (LinearLayout)v;
target.addView(view);
}
// make view visible as we set visibility to invisible while starting drag
view.setVisibility(View.INVISIBLE);
}
return true;
}
@Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
}
そしてactivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.condorpos.demoapp2.MainActivity">
<LinearLayout
android:id="@+id/left_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="@+id/box_view1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="10dp"
android:background="@drawable/box_one"/>
<ImageView
android:id="@+id/box_view2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="10dp"
android:background="@drawable/box_two"/>
<ImageView
android:id="@+id/box_view3"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="10dp"
android:background="@drawable/box_three"/>
<ImageView
android:id="@+id/box_view4"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="10dp"
android:background="@drawable/box_four"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center_vertical"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Thのためのanksみんな。
[Androidドラッグアンドドロップ機能](http://stackoverflow.com/questions/18725601/android-drag-and-drop-functionality)の可能な複製 –