【问题标题】:How to get the view while drag & drop in android?如何在android中拖放时获取视图?
【发布时间】:2015-03-07 06:59:49
【问题描述】:

我已经在我的应用程序中实现了拖放功能。我有四个 textview 并且我拖放。但是我想知道哪个 textview 被拖放了?我想知道哪个 textview 是放置目标和哪个 textview 是按我的代码中设计的名称拖动。

下面是代码。

视图的初始化代码

public void initView()

{
    p_one = (TextView) findViewById(R.id.tgoal_tvone);
    p_two = (TextView) findViewById(R.id.tgoal_tvtwo);
    p_three = (TextView) findViewById(R.id.tgoal_tvthree);
    p_four = (TextView) findViewById(R.id.tgoal_tvfour);
    query = (ImageView) findViewById(R.id.tgoal_img_query);
    tv_contentOne=(TextView)findViewById(R.id.tgoal_tvoneContent);
    tv_contentTwo=(TextView)findViewById(R.id.tgoal_tvtwoContent);
    tv_contentThree=(TextView)findViewById(R.id.tgoal_tvthreeContent);
    tvContentFour=(TextView)findViewById(R.id.tgoal_tvfourContent);

    query.setOnClickListener(this);


    //set touch listeners
    tv_contentOne.setOnTouchListener(new ChoiceTouchListener());
    tv_contentTwo.setOnTouchListener(new ChoiceTouchListener());
    tv_contentThree.setOnTouchListener(new ChoiceTouchListener());
    tvContentFour.setOnTouchListener(new ChoiceTouchListener());

    //set drag listeners
    tv_contentOne.setOnDragListener(new ChoiceDragListener());
    tv_contentTwo.setOnDragListener(new ChoiceDragListener());
    tv_contentThree.setOnDragListener(new ChoiceDragListener());
    tvContentFour.setOnDragListener(new ChoiceDragListener());



    /**set up the action bar**/

    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0099CC")));
    bar.setTitle(getResources().getString(R.string.tgoal_header));
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setHomeButtonEnabled(true);

}

获取drop view的代码

class ChoiceDragListener implements OnDragListener
    {
        int count=0;

        @Override
        public boolean onDrag(View v, DragEvent event) {
            // TODO Auto-generated method stub
            View dragView = (View) event.getLocalState();
            switch (event.getAction()) 
            {

            case DragEvent.ACTION_DRAG_STARTED:
                //no action necessary

                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                //no action necessary
                break;
            case DragEvent.ACTION_DRAG_EXITED:        
                //no action necessary
                break;
            case DragEvent.ACTION_DROP:

                //handle the dragged view being dropped over a drop view
                //handle the dragged view being dropped over a target view
                View view = (View) event.getLocalState();
                //stop displaying the view where it was before it was dragged
                //view dragged item is being dropped on
                TextView dropTarget = (TextView) v;
                //view being dragged and dropped
                TextView dropped = (TextView) view;
                String droppedTarget=dropTarget.getText().toString();
                 dropTarget.setText(dropped.getText());
                 dropped.setText(droppedTarget);

                //make it bold to highlight the fact that an item has been dropped
                dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
                dropTarget.setBackgroundColor(getResources().getColor(R.color.papya_orange));
                Log.i("log", "TV Dropped");
                if(dropTarget==p_one)
                {
                    Log.i("log", "Drop View is First TextView");

                }


                break;
            case DragEvent.ACTION_DRAG_ENDED:
                //no action necessary
                break;
            default:
                break;
        }
            return true;
        }

    }

【问题讨论】:

    标签: android drag-and-drop


    【解决方案1】:

    简单,只需在 onDrag() 回调中跟踪您的视图:

    private int mDragResourceId;//DECLARE THIS FIELD
    
        @Override
        public boolean onDrag(View v, DragEvent event) {
            // TODO Auto-generated method stub
            View dragView = (View) event.getLocalState();
            //ADD THIS LINE
            mDragResoruceId = dragView.getId();
    //REST OF YOUR CODE 
        }
    

    然后你可以使用这两种方法来跟踪你最后拖动的 View,都可以通过运行时资源 ID 和设计时资源名称:

    private int getDragResId(){
       return mDragResourceId;
    }
    
    private String getDragResName(){
       return getResources().getResourceName(getDragResId());
    }
    

    祝你好运。

    【讨论】:

    • 没错。但只是调用时的注意事项 v.startDrag(dragData, myShadow, v, 0);不要忘记在localState参数中传递需要拖动的v(视图)。我曾经将它传递给 null 所以当我尝试你的答案时它崩溃了,直到我意识到它应该通过 startDrag() 传递。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2013-03-04
    • 1970-01-01
    相关资源
    最近更新 更多