以alinear布局拖放图像

时间:2016-11-21 08:08:38

标签: android drag-and-drop imageview android-linearlayout

我在我的应用中尝试了一种模式。我在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"
 >

请帮助朋友。谢谢。

3 个答案:

答案 0 :(得分:1)

试试这个例子。添加listview或recyclerview并从列表中删除List项目,然后再次在项目对象的下方或上方添加项目....我还在处理拖放视图... http://android-er.blogspot.in/2015/01/drag-and-drop-items-between-listview_13.html < / p>

答案 1 :(得分:0)

XML文件:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/mLinearLayour"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF8989"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img1"
        android:layout_width="50dp"
        android:src="@mipmap/ic_launcher"
        android:layout_height="50dp"
         />

    <ImageView
        android:id="@+id/img2"
        android:layout_width="50dp"
        android:src="@mipmap/beauty"
        android:layout_height="50dp"
        />

    <ImageView
        android:id="@+id/img3"
        android:layout_width="50dp"
        android:src="@mipmap/icn_button"
        android:layout_height="50dp"
        />
    <ImageView
        android:id="@+id/img4"
        android:layout_width="50dp"
        android:src="@mipmap/nbcards"
        android:layout_height="50dp"
        />
    <ImageView
        android:id="@+id/img5"
        android:layout_width="50dp"
        android:src="@mipmap/ic_lock"
        android:layout_height="50dp"
        />
</LinearLayout>

源代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.View.DragShadowBuilder;
import android.widget.LinearLayout;
import android.util.Log;


public class MainActivity extends Activity implements       OnTouchListener,OnDragListener{
private static final String LOGCAT = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.img1).setOnTouchListener(this);
    findViewById(R.id.img2).setOnTouchListener(this);
    findViewById(R.id.img3).setOnTouchListener(this);
    findViewById(R.id.img4).setOnTouchListener(this);
    findViewById(R.id.img5).setOnTouchListener(this);

    findViewById(R.id.mLinearLayour).setOnDragListener(this);
}
public boolean onTouch(View view, MotionEvent motionEvent) { 
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
          DragShadowBuilder shadowBuilder = new DragShadowBuilder(view);
          view.startDrag(null, shadowBuilder, view, 0);
          view.setVisibility(View.INVISIBLE);
          return true;
        }
        else {
            return false;
        }
}  
public boolean onDrag(View layoutview, DragEvent dragevent) {
      int action = dragevent.getAction();
      switch (action) {
      case DragEvent.ACTION_DRAG_STARTED:
          Log.d(LOGCAT, "Drag event started");
        break;
      case DragEvent.ACTION_DRAG_ENTERED:
          Log.d(LOGCAT, "Drag event entered into "+layoutview.toString());
        break;
      case DragEvent.ACTION_DRAG_EXITED:
          Log.d(LOGCAT, "Drag event exited from "+layoutview.toString());
        break;
      case DragEvent.ACTION_DROP:
        Log.d(LOGCAT, "Dropped");
        View view = (View) dragevent.getLocalState();
        ViewGroup owner = (ViewGroup) view.getParent();
        owner.removeView(view);
        LinearLayout container = (LinearLayout) layoutview;
        container.addView(view);
        view.setVisibility(View.VISIBLE);
        //  view.invalidate();
        break;
      case DragEvent.ACTION_DRAG_ENDED:
              Log.d(LOGCAT, "Drag ended");
          break;
      default:
        break;
      }
      return true;
}

}

答案 2 :(得分:0)

在Action_Down案例中,只需添加此项就可以了。

                ImageView target=(ImageView)v;
                ImageView dragged = (ImageView) event.getLocalState();

                Drawable target_draw = target.getDrawable();
                Drawable dragged_draw = dragged.getDrawable();

                dragged.setImageDrawable(target_draw);
                target.setImageDrawable(dragged_draw);
                dragged.setVisibility(View.VISIBLE);
                target.setVisibility(View.VISIBLE);