拖动时无法看到新的framelayout阴影

时间:2016-01-26 14:05:16

标签: android drag-and-drop

我是初学Android开发者,我尝试制作小型纸牌游戏。 要使用Framelayout进行堆叠卡片,并在每个imageview内部代表一张卡片。 现在为了卡的拖放我成功实现了android"拖放"它适用于一张卡但如果我想拖动多个视图,我想要制作一个新的Framelayout,它将保持我按下和向前的所有图像视图(就像我按下8卡它将有8,7 ,6等等)但问题是我在拖动时无法看到新Framelayout的阴影。 这是我写的代码:

        //mfl is the name of the framelayout which holds all the cards (the stack)
    for (int i = 0; i < mfl.getChildCount()-1; i++) {
        final int place = i;
        mfl.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //creating the new framelayout to hold only the "chosen" cards
                FrameLayout viewgroup = new FrameLayout(Gamescreen.this);
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                viewgroup.setLayoutParams(layoutParams);
                //adding the new framelayout to the original (because if i dont do it i get an error 
                mfl.addView(viewgroup,0);

                int i = place;
                int childcount = mfl.getChildCount();
                ClipData clip = ClipData.newPlainText("type", "black");
                int steps = 0;
                switch (event.getAction()& MotionEvent.ACTION_MASK){
                    case MotionEvent.ACTION_DOWN:
                        Log.i("action", "down");
                        for (i = i; i <= childcount - 1; i++) {
                            Log.i("type", "i" + i);
                            Log.i("type", "child count " + mfl.getChildCount());
                            Log.i("type", "steps" + steps);
                            if (i!=0) {
                                ImageView child = (ImageView) mfl.getChildAt(i - steps);
                                mfl.removeViewAt(i - steps);
                                viewgroup.addView(child);
                                Log.i("type", "num of children in viewgroup" + viewgroup.getChildCount());
                                steps++;
                            }

                        }
                        viewgroup.invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.i("action", "move");
                        viewgroup.invalidate();
                        View.DragShadowBuilder shadow = new View.DragShadowBuilder(mfl);
                        v.startDrag(clip,shadow,viewgroup,0);
                        break;
                }
                return true;
            }
        });

如果我没有将新的framelayout添加到原始的这个

,我得到的错误
Unable to initiate drag
                                               java.lang.NullPointerException: Attempt to read from field 'android.view.IWindowSession android.view.View$AttachInfo.mSession' on a null object reference
                                                   at android.view.View.startDrag(View.java:19611)
                                                   at sagi.solitare.Gamescreen$1.onTouch(Gamescreen.java:134)
                                                   at android.view.View.dispatchTouchEvent(View.java:9296)
                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)

0 个答案:

没有答案