can't see new framelayout shadow while dragging

僤鯓⒐⒋嵵緔 提交于 2019-12-11 11:53:01

问题


i am a beginner android developer and i try to make a small solitaire game. to do the stacks of cards i use a Framelayout and inside each imageview represents a card. now for the drag and drop of the card i succeeded implementing the android "drag and drop" and it works for one card but if i want to drag more than one view i thought of making a new Framelayout which will hold all of the imageviews from where i pressed and onward (like if i pressed the 8 card it will have 8,7,6 and so forth) but the problem is that i cant see the shadow of the new Framelayout when dragging. this is the code i wrote:

        //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;
            }
        });

the error i get if i dont add the new framelayout to the original is this

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)

来源:https://stackoverflow.com/questions/35015676/cant-see-new-framelayout-shadow-while-dragging

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