我正在嘗試在我的應用程序中的模式。我在LinearLayout中有5個圖像。當我在其他位置拖放一個圖像時,圖像位置應該更新。 例如:拖放圖像在線形佈局
1 2 3 4 5 //suppose these are my images
我拾取5圖像拖放其間3,4- ..
滴下後序應該是這樣的:
1 2 3 5 4
我已經寫以下代碼:
import android.content.ClipData;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
float orix,origy;
LinearLayout llcontainer;
ImageView iv1,iv2,iv3,iv4;
ArrayList<Integer> alim;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llcontainer=(LinearLayout)findViewById(R.id.llcontainer);
iv1=(ImageView)findViewById(R.id.iv1);
iv2=(ImageView)findViewById(R.id.iv2);
iv3=(ImageView)findViewById(R.id.iv3);
alim=new ArrayList<>();
for(int i=0;i<5;i++)
{
ImageView iv=new ImageView(this);
iv.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
iv.setId(i);
iv.setImageResource(R.mipmap.ic_launcher);
alim.add(iv.getId());
iv.setOnDragListener(new MyDragListener());
iv.setOnTouchListener(new MyTouchListener());
llcontainer.addView(iv);
}
// alim.add(R.id.iv1);
// alim.add(R.id.iv2);
// alim.add(R.id.iv3);
// iv1.setOnTouchListener(new MyTouchListener());
// iv2.setOnTouchListener(new MyTouchListener());
//iv3.setOnTouchListener(new MyTouchListener());
//findViewById(1).setOnDragListener(new MyDragListener());
//iv2.setOnDragListener(new MyDragListener());
//iv3.setOnDragListener(new MyDragListener());
llcontainer.setOnDragListener(new MyDragListener());
/* findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
findViewById(R.id.topright).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomright).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);
origy=view.getY();
orix=view.getX();
return true;
} else {
return false;
}
}
}
class MyDragListener implements View.OnDragListener {
/*Drawable enterShape = getResources().getDrawable(
R.drawable.shape_droptarget);
Drawable normalShape = getResources().getDrawable(R.drawable.shape);
*/
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
// Toast.makeText(getApplicationContext(),"drag started",Toast.LENGTH_LONG).show();
break;
case DragEvent.ACTION_DRAG_ENTERED:
// v.setBackgroundDrawable(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
// v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
float X = event.getX();
float Y = event.getY();
int id=v.getId();
// for(int i=0;i<alim.size();i++)
// {
//if(id==alim.get(i))
// {
View view = (View) event.getLocalState();
int owner=(int)view.getId();
if(alim.size()==owner+1)
{
Toast.makeText(getApplicationContext(),"matched",Toast.LENGTH_LONG).show();
ImageView v1=(ImageView) llcontainer.getChildAt(owner);
v1.setX(X);
v1.setY(Y);
v1.setVisibility(View.VISIBLE);
ImageView ig=(ImageView)llcontainer.findViewById(owner-1);
ig.setX(orix);
ig.setY(origy);
ig.setVisibility(View.VISIBLE);
//ig
}
// llcontainer.addView(ig);
// ig.setX(X);
//ig.setY(Y);
// }
// }
/*
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
if(X>container.getX()&&Y>container.getY())
{
container.setOrientation(LinearLayout.HORIZONTAL);
// int position=owner.indexOfChild(v);
// view.setX(X);
// view.setY(Y);
view.setX(X-(view.getWidth()/2));
view.setY(Y-(view.getHeight()/2));
container.addView(view);
view.setVisibility(View.VISIBLE);
}
else
{
owner.addView(view);
view.setX(orix);
view.setY(origy);
view.setVisibility(View.VISIBLE);
}*/
break;
case DragEvent.ACTION_DRAG_ENDED:
// v.setBackgroundDrawable(normalShape);
default:
break;
}
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在我的activity_main.xml中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/llcontainer"
android:orientation="horizontal"
>
請幫幫忙啦。提前感謝。
請參鏈接http://stackoverflow.com/questions/25055292/android-drag-and-drop-imageview-ontouchlistener – Vadivel
我都試過,但沒有用我.. –
哦歐凱,您可以使用gridview的移動圖像 – Vadivel