Android: drag and drop problem

I am writing SMIL composer for the class, and I planned to make the ink support upholstery and drop so that you can place the images and text as you want. I looked through the examples and made some of my own, but when I go to implement drag and drop in my project, this will not work. Here is the main code that matters:

public class ComposerActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.composer); Button add = (Button)findViewById(R.id.addBtn); ... add.setOnClickListener(mClick); ... } OnClickListener mClick = new OnClickListener() { @Override public void onClick(View v){ if(v.getId() == R.id.addBtn) { FrameLayout fl = (FrameLayout)findViewById(R.id.Canvas); LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.image_add, null); View image = (View)itemView.findViewById(R.id.image); image.setBackgroundResource(R.drawable.icon); fl.addView(itemView, new FrameLayout.LayoutParams(40, 40)); image.setOnTouchListener(drag); } ... OnTouchListener drag = new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event){ FrameLayout.LayoutParams par = (LayoutParams) v.getLayoutParams(); switch(v.getId()){//What is being touched case R.id.image:{//Which action is being taken switch(event.getAction()){ case MotionEvent.ACTION_MOVE:{ par.topMargin = (int)event.getRawY() - (v.getHeight()); par.leftMargin = (int)event.getRawX() - (v.getWidth()/2); v.setLayoutParams(par); break; }//inner case MOVE case MotionEvent.ACTION_UP:{ par.height = 40; par.width = 40; par.topMargin = (int)event.getRawY() - (v.getHeight()); par.leftMargin = (int)event.getRawX() - (v.getWidth()/2); v.setLayoutParams(par); break; }//inner case UP case MotionEvent.ACTION_DOWN:{ par.height = 60; par.width = 60; v.setLayoutParams(par); break; }//inner case UP }//inner switch break; }//case image }//switch return true; }//onTouch };//drag } 

Here is the .xml composer:

 ... <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/TopBar" android:layout_above="@+id/addBtn" android:id="@+id/Canvas" android:layout_gravity="top"> </FrameLayout> ... 

and image_add.xml:

 <View android:id="@+id/image" android:layout_gravity="top" xmlns:android="http://schemas.android.com/apk/res/android"/> 

When I click the “Add” button on my composer, it successfully adds the image to the canvas, and when I touch the image, it responds to magnification, from 40x40 to 60x60, as it should. But he does not follow my finger across the screen, and that’s where I got stuck.

Any input is welcome.

+7
source share
2 answers

I studied the Android Launcher code to find out how they dragged. I really like their object model. What they did, and it may be useful for you, is to take responsibility for handling the drag and drop in the ViewGroup, in which all the drag and drop views are inside. Touching events is processed there, and not according to your ideas.

Two key classes:

DragLayer - implements a custom ViewGroup that coordinates the movement of views on the screen. Launcher DragLayer is a subclass of FrameLayout.

DragController - This object is a controller that does most of the work to support drag and drop.

Another thing they do is not move the actual view until you drop the object you are dragging. It doesn’t look like this because on the screen you see a bitmap built from a view that moves as you move your finger (or pointer).

I built a simple example and wrote it. See http://blahti.wordpress.com/2011/01/17/moving-views-part-2/ An example source code also exists.

+14
source

If you are looking for a turnkey solution, do the following:

https://github.com/thquinn/DraggableGridView

This is the custom ViewGroup I am building, it looks like it is suitable for your project. Good luck

+1
source

All Articles