0

我有 4 个Fragments,一个带有 3 个按钮和 3 个画布片段的导航片段。因为我需要计算layoutFragment以前onfocuschangedlistenercanvas.

view.getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() 
{
    @Override
    public void onGlobalFocusChanged(View oldFocus, View newFocus) 
    {
            //calculate width and height of available space 
            //instansiate view object 
            layout.removeAllViews();
            layout.addView(view object);
    }
});

这在第一次加载应用程序时工作正常,并且我默认加载视图,但之后一旦我开始通过导航片段在片段之间切换,视图就永远不会重绘。我发现onglobalfocuschange 即使我加载另一个也不会再调用它Fragment。我怎样才能做到这一点。感谢任何建议。

这是我绘制条形图的视图类。现在我只是使用常量值进行测试。

public class bargraphClass extends View
    {
        int startX,startY,stopX,stopY;
        int leftSp,bottomSp;
        RectF fillRect;
        float hrs;
        float ppm;
        Paint rectBrush=new Paint();
        int g1,g2,g3,g4;
        public bargraphClass(Context context,int wt,int ht) 
        {
            super(context);
            leftSp=wt/5;
            bottomSp=ht/20;
            startX=leftSp;
            startY=0;
            stopX=wt;
            stopY=ht-bottomSp;
            fillRect=new RectF();
            g1=5*60;
            g2=4*60;
            g3=8*60;
            g4=6*60;
        }


        @Override
        protected void onDraw(Canvas c) 
        {
            // TODO Auto-generated method stub
            int _wt,gap,temp;       
            super.onDraw(c);
            drawAxis(c);
            ppm=(float)(stopY-startY)/(24*60);
            _wt=stopX-startX;
            gap=_wt/9;

            temp=(int) (g1*ppm);
            fillRect.set(startX+gap, stopY-temp, startX+(gap*2), stopY);            
            rectBrush.setColor(Color.RED);
            rectBrush.setAntiAlias(true);
            rectBrush.setAlpha(128);
            c.drawRect(fillRect,rectBrush);

            temp=(int) (g2*ppm);
            fillRect.set(startX+(gap*3), stopY-temp, startX+(gap*4), stopY);            
            rectBrush.setColor(Color.GREEN);
            rectBrush.setAntiAlias(true);
            rectBrush.setAlpha(128);
            c.drawRect(fillRect,rectBrush);

            temp=(int) (g3*ppm);
            fillRect.set(startX+(gap*5), stopY-temp, startX+(gap*6), stopY);            
            rectBrush.setColor(Color.BLUE);
            rectBrush.setAntiAlias(true);
            rectBrush.setAlpha(128);
            c.drawRect(fillRect,rectBrush);

            temp=(int) (g4*ppm);
            fillRect.set(startX+(gap*7), stopY-temp, startX+(gap*8), stopY);            
            rectBrush.setColor(Color.MAGENTA);
            rectBrush.setAntiAlias(true);
            rectBrush.setAlpha(128);
            c.drawRect(fillRect,rectBrush);

        }

        private void setScaleValues()
        {
            ppm=stopY/(24*60);
        }

        private void drawAxis(Canvas c)
        {
            int sp;
            String str;
            Paint p=new Paint();
            p.setColor(Color.DKGRAY);
            p.setStrokeWidth(2);
            p.setAntiAlias(true);

            c.drawLine(startX,startY,startX,stopY, p);
            c.drawLine(startX, stopY, stopX, stopY, p);

            sp=(stopY-startY)/12;
            for(int i=1;i<12;i++)
            {
                str=String.valueOf(i*2)+" hrs";
                c.drawText(str, 0, ht-(i*sp)-bottomSp,p);
            }
        }
    }
4

0 回答 0