如何注册的dragEvent,而里面已经之一,有它在当前的dragEvent听呢? [英] How to register a DragEvent while already inside one and have it listen in the current DragEvent?

查看:175
本文介绍了如何注册的dragEvent,而里面已经之一,有它在当前的dragEvent听呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是涉及到Android中的拖放API。我很抱歉,如果我的问题是混乱的。但实际上,当你要开始一个拖放操作,你必须调用的startDrag()查看。您通过它的一些信息,然后你给它一个 OnDragListener 在其中侦听查看这是拖动。当拖动开始,在我的例子很长的preSS,所有可以接受任何方式的意见是注册系统接受这些信息。它会更有意义,如果你快速通过一些这在Android文档来的拖放。但是,多数民众赞成它是如何工作的假设你还没有使用过它,并想快速浏览。

My question is is related to the Drag and Drop API in Android. I apologize if my question was confusing. But essentially, when you want to start a Drag and Drop operation, you must invoke startDrag() on a View. You pass it in some info, and then you give it a OnDragListener in which to listen for a View which is being dragged. When the a drag is started, in my example a long press, all the Views that can accept any info are registered with the system to accept this info. It would make more sense if you quickly read through some of this in the android docs for Drag and Drop. But thats a quick overview of how it works assuming you havent used it before and would like to.

现在我的问题基本上是我所遇到的问题。我想创建一个查看,而 onDragListener 正在运行也接受系统的信息上正在拖查看。问题是它不能正常工作。我并不惊讶,因为拖动操作刚开始的时候,当查看未注册。现在的问题是我怎么能注册它的飞行。 android的文档展示了如何将其​​注册在开始而不是当它已经之一。

Now my question is essentially in an issue I have come across. I would like to create a View while the onDragListener is running that also accepts system info on a currently being dragged View. Problem is its not working. I am not surprised as the View was not registered when when the Drag operation first started. The question is how can I register it on the fly. The android docs shows you how to register it in the beginning but not when its already in one.

MyDragListener (code我onDragListener。现在只玩了。)

MyDragListener (Code for my onDragListener. Right now only playing with it.)

protected class MyDragListener implements View.OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {

        // Defines a variable to store the action type for the incoming event
        final int action = event.getAction();

        // Handles each of the expected events
            switch(action) {

                case DragEvent.ACTION_DRAG_STARTED:
                    // Determines if this View can accept the dragged data
                    /*The way it works is it essentially turns anything that can accept stuff to Blue 
                     * or whatever color. It also does not need to do anything in terms of showing it can accept anything.*/
                    if(event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                        v.setBackgroundColor(Color.BLUE);

                        return (true);

                    } else {
                        return (false);
                    }
                //break;

                case DragEvent.ACTION_DRAG_ENTERED:
                    v.setBackgroundColor(Color.YELLOW);
                    Toast.makeText(v.getContext(), "Height is: "+v.getHeight(), Toast.LENGTH_SHORT).show();
                    return true;
                //break;

                case DragEvent.ACTION_DRAG_LOCATION:
                    //this is triggered right after ACTIN_DRAG_ENTERED
                    Log.d("TestActivity", "Location of the View you are dragging is x: "+event.getX()+" y: "+event.getY());
                    if(v.getHeight()-10 < event.getY() && v.getHeight() > event.getY()&& showingView == false ) {

                            //here you are checking if its in the lower bound of the view
                            LayoutInflater inflater = LayoutInflater.from(v.getContext());
                            LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.some_text, table, false);
                            ((TextView)layout.findViewById(R.id.da_text)).setText("Some text");
                            layout.setTag("Empty");
                            layout.startDrag(ClipData.newPlainText("Empty", "Empty"), new View.DragShadowBuilder(), null, 0);
                            table.addView(layout, Integer.parseInt(v.getTag().toString())+1);
                        showingView = true;
                    }
                    return true;


                case DragEvent.ACTION_DRAG_EXITED:
                    v.setBackgroundColor(Color.BLUE);
                    return true;
                //break;

                case DragEvent.ACTION_DROP:
                    //this is obvious, gets the data in the view that was dropped
                    return true;

                case DragEvent.ACTION_DRAG_ENDED:
                    v.setBackgroundColor(Color.WHITE);
                    return true;


            }//end of switch

        return false;
    }

}//end of MyDragListener

修改1

于是,我尝试了不同的方法。我决定要持有我的的LinearLayout 这将是在 TableLayout 的项目。这样,我就注册了 OnDragListener 然后我可能只是拿着它,直到我需要它。这失败。我第一次用它,即使我把它添加到TableLayout,也不会听任何一拖再拖,但是当我放弃了拖再长pressed它,它的工作。因此,它是与一个事实,即它的无形的,然后它不是。因此,在默认情况下它不会当它的隐形做任何事情,我不知道如何使不会发生,并让我把它做一些事情,即使它不可见。任何想法?

So I tried a different approach. I decided to have a holder for my LinearLayout which would be an item in the TableLayout. That way I would register the OnDragListener then I could just hold it until I needed it. That failed. The first time I used it, even if I added it to the TableLayout, it would not Listen to any drags, but when I dropped the drag and then long pressed it, it worked. So it has something to do with the fact that its invisible, and then its not. So by default it does not do anything when its invisible, and I am not sure how to make that not happen, and allow me to have it do something even when its invisible. Any ideas?

推荐答案

我知道这是一个老的文章,但如果有人需要一个解决方案/劈... 我一直在努力与这个了几个小时,并认为这可能是有用的人。

I know this is an old post but if somebody needs a solution/hack... I had struggled with this for a couple of hours and thought that it might be useful for someone.

我的问题

我,当我试图让我的ListView向上/向下滚动,而我拖着一个项目这个问题。问题是,滚动时,列表视图会加载新改建欣赏到组视图和那些不活跃的同时拖动开始(他们在列表视图缓存 - 而不是有关儿童的视图组)没有回应向拖动。

I got this problem when I was trying to have my ListView scroll up/down while I was dragging an item. The issue was that when scrolling, the listview would load new converted views to the group view and the ones that were not active while the drag started (they were in the listview cache - and not associated as children for the view group) did not respond to the dragging.

您有关于它的正确的想法,因为视图错过了最初的DRAG_STARTED事件则没有得到任何其他活动。

You had the right idea about it, because the view missed the initial DRAG_STARTED event then it wasn't getting any other events.

我要通过源$ C ​​$下查看/视图组,还有我们能破解这件事的几种方法。最后,我决定通过强制能见度变化问题的看法,这将启动一连串的事件,最终将包括在未来拖动事件的观点来解决它调用

I was going through the source code for VIEW/VIEW GROUP and there are a few ways that we can hack this thing. Eventually I decided to solve it by forcing a visibility change for the problematic view, which will start a chain of events that will eventually include the view in future DRAG EVENTS calls

解决方案:能见度变化破解

因此​​,在你必须附着于母只需设置可见性消失了,回到可见新的观点:

So after you have the new view attached to the parent simply set visibility to GONE and back to VISIBLE:

child.setVisibility(View.GONE);
child.setVisibility(View.VISIBLE);

注意:这样做而)的用户仍然拖二)新视图已添加到父

Note: do this while a) the user is still dragging b) the new view is already added to a parent.

注意:对我来说,这是足以解决问题。但,在我的情况我在同一个容器中已经接受拖动事件等的看法。如果这是第一个孩子在该容器中可能无法正常工作。你可以尝试做同样的伎俩,以集装箱,以及集装箱的容器,依此类推,直到你达到一个容器,它必须接受事件的孩子。

Note: For me this was enough to solve the problem. BUT, in my case I had other views in the same container that were already accepting DRAG EVENTS. If this is the first child in that container it might not work. You can try to do the same trick to the container ,and to the container's container, and so on until you reach a container that had children that accept the EVENTS.

这篇关于如何注册的dragEvent,而里面已经之一,有它在当前的dragEvent听呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆