Android - 为每个孩子设置不同的OnTouch事件

时间:2013-01-21 09:17:49

标签: android android-layout ontouchlistener

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

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个动态添加视图,所以我的代码将添加2个视图,每个循环从1和2到7和8但是传递给cbDrag.onDrag的内容总是第7和第8个数据所以当我试图拖动图像会显示的是第7和第8张图片,哪里出错了?当我试图设置ontouch监听器时,数据将始终被更改,因为我在循环中有它?有什么方法可以解决这个问题吗?

谢谢

0 个答案:

没有答案