Drag & Drop + Android Custom Drawing

I'm working on what requires custom drag and drop functions, so I subclassed the View by doing a bunch of math in response to touch events, and then manually outputting everything using the canvas code in onDraw. Now, the more functionality I add, the more code gets out of hand, and I find that I write more tons of code than I would expect in a high-level environment like Android.

Is this so, or am I missing something? If I am not doing anything unusual in the user interface, the structure handles most of my interactions. Built-in controls handle strokes and drag and drop, and my code is pretty much limited to business logic and data. Is there a way to take advantage of some of the user interface controls and things like animation, and also make some of them manually on the onDraw canvas? Is there an accepted standard for when to use one or the other (if indeed the two approaches can be mixed)?

+6
java android user-interface drag-and-drop
source share
1 answer

I use drag and drop in music player app! I give the user the ability to move the song from the playlist to another playlist. It is really nice and easy for the user. I fire a drag event to view when the user makes a long click on a song or when an option is selected from the menu! This is my class:

package com.liviu.app.smpp.gui; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.RelativeLayout; import android.widget.TextView; import com.liviu.app.smpp.R; import com.liviu.app.smpp.listeners.CollisionListener; public class SongItemView extends RelativeLayout implements OnClickListener { // data private String TAG = "SongItemView"; private Context context; private LayoutInflater lInflater; private String title; private int id; private int maxHeight = 410; private int mCurX; private int mCurY; //listeners private CollisionListener onCollisionListener = null; // views private View v; public SongItemView(Context ctx, String title_, int id_) { super(ctx); context = ctx; lInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = lInflater.inflate(R.layout.song_item_view, null); title = title_; id = id_; ((TextView)v.findViewById(R.id.siv_title)).setText(title); addView(v, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } @Override public void onClick(View v) { Log.e(TAG, "clicked! " + ((TextView)v.findViewById(R.id.piv_title)).getText().toString()); } public View getView(){ return v; } public String getPlsName() { return title; } public int getID() { return id; } public void setTitle(String title_){ ((TextView)v.findViewById(R.id.siv_title)).setText(title_); title = title_; } public void setID(int id_) { id = id_; } @Override public boolean dispatchTouchEvent(MotionEvent event) { mCurX = (int) event.getRawX(); mCurY = (int) event.getRawY(); int action = event.getAction(); if (action == MotionEvent.ACTION_MOVE) { RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.leftMargin = mCurX; params.topMargin = mCurY; this.setLayoutParams(params); if(this.getTop() >= maxHeight) { Log.e(TAG, "Collision!!!!"); if(onCollisionListener != null){ onCollisionListener.onCollision(this); } } } return true; } public void setOnCollisionListener(CollisionListener listener){ onCollisionListener = listener; } public void setMaxHeight(int height){ maxHeight = height; } public int getmCurX() { return mCurX; } public int getmCurY() { return mCurY; } public int getMaxHeight() { return maxHeight; } 

}

Hope this helps a bit.

Thanks!

+7
source share

All Articles