Android - how to simply drag and drop a button?

戏子无情 提交于 2019-12-08 09:33:38

问题


I found a beginner-friendly tutorial here: http://androidrox.wordpress.com/2011/05/13/android-sample-app-drag-and-drop-image-using-touch/

here is the xml code in my case:

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

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Countries" />

</AbsoluteLayout>

MainActivity.java:

public class MainActivity extends Activity {

AbsoluteLayout absLayout;
Button myButton = null;
boolean touched = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    absLayout = (AbsoluteLayout) findViewById(R.id.absLayout);
    myButton = (Button) findViewById(R.id.myButton);

    myButton.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            touched = true;
            return false;
        }
    });

    absLayout.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(touched == true) // any event from down and move
            {
                LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-button_Countries.getWidth()/2,(int)event.getY()-myButton.getHeight()/2);
                myButton.setLayoutParams(lp);
            }
            if(event.getAction()==MotionEvent.ACTION_UP){
                touched = false;
            }
            return true;
        }
    });
}

Basically what happens here I need to touch myButton once first to convert the boolean touched to true. Then I might be able to drag myButton by start touching anywhere on absLayout. How can I simply drag the button freely without the need of touching the button first?

I tried setting the LayoutParams on myButton touch listener. It causes flickering to the dragging progress.

Also, AbsoluteLayout is always tagged as deprecated. What does "deprecated" means? Does it mean obsolete and therefore unusable? Or usable but there might be a better solution?


回答1:


myButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        touched = true;
        return false;
    }
});

The above part is responsible for touch that enables your button to be dragged. Now remove this part of the code and change the declaration to boolean touched = true;

Now you should be able to drag without touching the button first.



来源:https://stackoverflow.com/questions/20610288/android-how-to-simply-drag-and-drop-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!