如何在android中为以下代码创建动态for循环?

时间:2014-03-31 18:40:34

标签: android android-layout android-fragments android-fragmentactivity

我正在尝试为几个重复的块做一个for循环,但是,它们的一些值在xy位置上有所不同,我如何使它们仍然可以使用for循环以某种方式管理for循环中不同的xy位置。这是我的代码:

m_views.add(hm7);

        HeatMapElementView hm8 = (HeatMapElementView)view.findViewById(R.id.hmEle_08);
        hm8.setTag(new Integer[]{Integer.valueOf(4),Integer.valueOf(2)});
        hm8.setOnClickListener(
                new OnClickListener(){
                    @Override
                    public void onClick(View vw){
                        if(isAnimating)
                            return;

                        RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)m_hoverCraft.getLayoutParams();

                        //int xColumn = ((Integer[])vw.getTag())[0].intValue();
                        relParams.leftMargin = ((int)vw.getX())+((int)vw.getWidth()) - 450;
                        int yRow = ((Integer[])vw.getTag())[1].intValue();
                        relParams.topMargin = ((int)(vw.getHeight()*yRow) - (vw.getHeight()/2) - 162);

                        m_hoverCraft.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                        m_hoverCraft.setTag(new float[]{1.0f,0.5f});
                        Animation anim = new ScaleAnimation(
                                0f, 1f, // Start and end values for the X axis scaling
                                0f, 1f, // Start and end values for the Y axis scaling
                                Animation.RELATIVE_TO_SELF, 1.0f, // Pivot point of X scaling
                                Animation.RELATIVE_TO_SELF, 0.5f);
                        showHoverView(vw,anim);
                    }
                }
        );
        //hm8.setOnHoverListener(heatMapHover);
        m_views.add(hm8);

        HeatMapElementView hm9 = (HeatMapElementView)view.findViewById(R.id.hmEle_09);
        hm9.setTag(new Integer[]{Integer.valueOf(1),Integer.valueOf(3)});
        hm9.setOnClickListener(
                new OnClickListener(){
                    @Override
                    public void onClick(View vw){
                        if(isAnimating)
                            return;

                        RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)m_hoverCraft.getLayoutParams();

                        //int xColumn = ((Integer[])vw.getTag())[0].intValue();
                        relParams.leftMargin = (int)vw.getX();

                        int yRow = ((Integer[])vw.getTag())[1].intValue();
                        relParams.topMargin = ((int)(vw.getHeight()*yRow) - (vw.getHeight()/2) - 162);

                        m_hoverCraft.setTag(new float[]{0.0f,0.5f});
                        m_hoverCraft.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                        Animation anim = new ScaleAnimation(
                                0f, 1f, // Start and end values for the X axis scaling
                                0f, 1f, // Start and end values for the Y axis scaling
                                Animation.RELATIVE_TO_SELF, 0.0f, // Pivot point of X scaling
                                Animation.RELATIVE_TO_SELF, 0.5f);
                        showHoverView(vw,anim);
                    }
                }
        );
        //hm9.setOnHoverListener(heatMapHover);
        m_views.add(hm9);

        HeatMapElementView hm10 = (HeatMapElementView)view.findViewById(R.id.hmEle_10);
        hm10.setTag(new Integer[]{Integer.valueOf(2),Integer.valueOf(3)});
        hm10.setOnClickListener(
                new OnClickListener(){
                    @Override
                    public void onClick(View vw){
                        if(isAnimating)
                            return;

                        RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)m_hoverCraft.getLayoutParams();

                        //int xColumn = ((Integer[])vw.getTag())[0].intValue();
                        relParams.leftMargin = (int)vw.getX() + (int)vw.getWidth()/2 - 225;

                        int yRow = ((Integer[])vw.getTag())[1].intValue();
                        relParams.topMargin = ((int)(vw.getHeight()*yRow) - (vw.getHeight()/2) - 162);

                        m_hoverCraft.setTag(new float[]{0.5f,0.5f});
                        m_hoverCraft.setGravity(Gravity.CENTER);
                        Animation anim = new ScaleAnimation(
                                0f, 1f, // Start and end values for the X axis scaling
                                0f, 1f, // Start and end values for the Y axis scaling
                                Animation.RELATIVE_TO_SELF, 0.5f, // Pivot point of X scaling
                                Animation.RELATIVE_TO_SELF, 0.5f);
                        showHoverView(vw,anim);
                    }
                }
        );

1 个答案:

答案 0 :(得分:0)

只需将其中一个块包装成简单的for循环即可。例如,您可以创建一个包含所有变量的对象,并创建这些对象的数组。 或者你可以创建浮点数组(或其他任何东西)..像这样

int params[] =  {1, 2, 3, 4, 5};


for (int i = 0; i < 4; i++) {

  //your code
  // .
  // .
  // .
  someFunctionWithParameter(params[i]);
  // .
  // .
  //.

}