2

您好我正在尝试创建一个拖放视图并且我已经成功但是我有一个问题,我像这样动态添加我的视图

int k=0;
        for(int i=0;i<badge_list_id.size()/2;i++) {
            Log.i(BaseID.TAG, "K : "+k);
            temp_badge_list_id.clear();
            temp_badge_list_name.clear();
            temp_badge_list_url.clear();
            for(int j=k;j<k+2;j++)
            {
                temp_badge_list_id.add(badge_list_id.get(j));
                temp_badge_list_name.add(badge_list_name.get(j));
                temp_badge_list_url.add(badge_list_url.get(j));
            }
            llContainerBadgeList.addView(new ChildPopUpTopSend(context, temp_badge_list_id, temp_badge_list_name,
                    temp_badge_list_url, badge_id, new callbackDragDrop()));
            k=k+2;
        }

并像这样在子视图中设置 ontouch 侦听器

private View.OnTouchListener mOnTouchDrag1=new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction())
            {
                case MotionEvent.ACTION_MOVE:
                {
                    cbDrag.onDrag(badge_list_id.get(0), badge_id, BaseID.DRAGGING, event, v, badge_list_url.get(0));
                    break;
                }//inner case MOVE
                case MotionEvent.ACTION_UP:
                {
                    cbDrag.onDrag(badge_list_id.get(0), badge_id, BaseID.STOP_DRAG, event, v, badge_list_url.get(0));
                    break;
                }//inner case UP
                case MotionEvent.ACTION_DOWN:
                {
                    cbDrag.onDrag(badge_list_id.get(0), badge_id, BaseID.START_DRAG, event, v, badge_list_url.get(0));
                    break;
                }//inner case UP
            }//inner switch

            return true;
        }
    };

而cbDrag.onDrag函数是这样的

private class callbackDragDrop implements callbackDrag {

        @Override
        public void onDrag(int selected_badge, ArrayList<Integer> badge_id, int dragState, 
                MotionEvent event, View v, String badge_url) {
            LinearLayout.LayoutParams par = (LinearLayout.LayoutParams) v.getLayoutParams();
            switch(dragState) {
            case BaseID.START_DRAG:
                llContainerDrag = new LinearLayout(context);
                llContainerDrag.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
                        (Util.getDisplayHeight(context)*8)/10));
                ivDrag = new ImageView(context);
                imageLoader.DisplayImage(badge_url, ivDrag);
                par.topMargin = (int)event.getRawY() - (int)(v.getHeight()*1.5);
                par.leftMargin = (int)event.getRawX() - (int)(v.getWidth()*1.5);
                ivDrag.setLayoutParams(par);
                llContainerDrag.addView(ivDrag);
                PopUpTopSend.this.addView(llContainerDrag);
                break;
            case BaseID.DRAGGING:
                par.height = drag_height;
                par.width = drag_width;
                par.topMargin = (int)event.getRawY() - (int)(v.getHeight()*1.5);
                par.leftMargin = (int)event.getRawX() - (int)(v.getWidth()*1.5);
                ivDrag.setLayoutParams(par);
                break;
            case BaseID.STOP_DRAG:
                int drop_position = checkDropLocation(event);
                Log.i(BaseID.TAG, "Drop position : "+drop_position);
                ImageView ivDrop;
                switch(drop_position) {
                case 1:
                    if(selected_badge!=badge_id.get(0)) {
                        ivDrop = (ImageView) findViewById(R.id.ivBadgeSend1);
                        imageLoader.DisplayImage(badge_url, ivDrop);
                        PopUpTopSend.this.badge_id.remove(0);
                        PopUpTopSend.this.badge_id.add(0, selected_badge);
                    }
                    break;
                case 2:
                    if(selected_badge!=badge_id.get(1)) {
                        ivDrop = (ImageView) findViewById(R.id.ivBadgeSend2);
                        imageLoader.DisplayImage(badge_url, ivDrop);
                        PopUpTopSend.this.badge_id.remove(1);
                        PopUpTopSend.this.badge_id.add(1, selected_badge);
                    }
                    break;
                case 3:
                    if(selected_badge!=badge_id.get(2)) {
                        ivDrop = (ImageView) findViewById(R.id.ivBadgeSend3);
                        imageLoader.DisplayImage(badge_url, ivDrop);
                        PopUpTopSend.this.badge_id.remove(2);
                        PopUpTopSend.this.badge_id.add(2, selected_badge);
                    }
                    break;
                }
                llContainerDrag.removeView(ivDrag);
                PopUpTopSend.this.removeView(llContainerDrag);
                break;
            }
        }

    }

假设我要动态添加 8 个视图,因此我的代码将从 1 和 2 到 7 和 8 的每个循环添加 2 个视图,但是传递给 cbDrag.onDrag 的始终是第 7 和第 8 数据,所以当我尝试拖动图像时将显示的是第 7 和第 8 图像,我哪里出错了?是不是当我尝试设置 ontouch 侦听器时,数据总是会因为我在循环中而被更改?有没有办法解决这个问题?

谢谢你

4

0 回答 0