5

我正在尝试实现 android 的拖放示例。长按一个项目后,我将它拖到目标级别并放在那里。我在某种程度上是成功的。但我想要一些不同的东西。我做过这样的事情.. 在此处输入图像描述

但我想将所有项目添加到 listView ,长按 listview 后我会选择一个项目,然后我会将项目拖到我的目标级别并将其放在那里。我想要这样的东西.. 在此处输入图像描述

我的代码如下...

public class MainActivity extends Activity {



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






    findViewById(R.id.mango).setOnLongClickListener(longListen);
    findViewById(R.id.orange).setOnLongClickListener(longListen);
    findViewById(R.id.apple).setOnLongClickListener(longListen);
    findViewById(R.id.banana).setOnLongClickListener(longListen);

    findViewById(R.id.drop_here).setOnDragListener(DropListener);

    listview.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            // TODO Auto-generated method stub

            Log.i("long clicked","pos"+" "+pos);

            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.main, menu);
    return true;
}

OnLongClickListener longListen = new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        DragShadow dragShadow = new DragShadow(v);

        ClipData data = ClipData.newPlainText("", "");

        v.startDrag(data, dragShadow, v, 0);

        return false;
    }
};

private class DragShadow extends View.DragShadowBuilder {

    ColorDrawable greyBox;

    public DragShadow(View view) {
        super(view);
        // TODO Auto-generated constructor stub
        greyBox = new ColorDrawable(Color.LTGRAY);
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        // TODO Auto-generated method stub
        // super.onDrawShadow(canvas);

        greyBox.draw(canvas);
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize,
            Point shadowTouchPoint) {
        // TODO Auto-generated method stub
        // super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
        View v = getView();

        int height = (int) v.getHeight() / 2;
        int width = (int) v.getWidth() / 2;

        greyBox.setBounds(0, 0, width, height);
        shadowSize.set(width, height);

        shadowTouchPoint.set(width / 2, height / 2);

    }


}

OnDragListener DropListener = new View.OnDragListener() {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        // TODO Auto-generated method stub

        int dragEvent = event.getAction();

        switch (dragEvent) {
        case DragEvent.ACTION_DRAG_ENTERED:
            Log.i("Drag ", "Entered");
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            Log.i("Drag ", "Exit");
            break;
        case DragEvent.ACTION_DROP:
            Log.i("Drag ", "Drop");

            TextView target = (TextView) v;
            TextView dragg = (TextView) event.getLocalState();
            target.setText(dragg.getText());
            break;
        default:
            break;
        }

        return true;
    }
};

}

我已经尝试了一些教程,但我无法从那里获得解决方案。 Android列表视图拖放排序

4

2 回答 2

6

我找到了解决方案。很简单,只需调用 setOnItemClickListener 中的 startDrag 方法即可。

listview.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View v,
                int pos, long id) {
            // TODO Auto-generated method stub


            v.startDrag(data, dragShadow, v, 0);
            return true;
        }
    });
于 2013-07-04T11:32:43.490 回答
0

Lars Vogel在这里有很好的教程 http://www.vogella.com/articles/AndroidDragAndDrop/article.html 。但它仅适用于 Android 4 及更高版本(在 ICS 之后)。

如果那没有给您解决方案,请尝试添加一个简单的 onTouchListener 来拖动对象,并查看拖动时对象是否在您要拖动到的视图的范围内(在您的情况下为文本视图)

可以在这里找到一个简单的 Drag ontouchlistener 实现:https ://stackoverflow.com/a/9398861/2138983

于 2013-07-03T07:20:23.007 回答