Android 当我切换片段时画布没有重新绘制?

Android 当我切换片段时画布没有重新绘制?,android,canvas,android-fragments,Android,Canvas,Android Fragments,我有4个片段,一个带有3个按钮的导航片段和3个画布片段。由于我需要计算片段中布局的大小,我使用onfocuschangedlistener在画布上绘制 view.getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() { @Override public void onGlobalFocusChanged(View oldFocus, View newFocus

我有4个
片段
,一个带有3个按钮的导航片段和3个画布片段。由于我需要计算
片段中
布局的大小,我使用
onfocuschangedlistener
画布上绘制

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
。我怎样才能做到这一点。感谢您的建议

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

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);
            }
        }
    }
公共类bargraphClass扩展视图
{
int startX、startY、stopX、stopY;
int leftSp,bottomSp;
RectF fillRect;
浮动小时数;
浮动ppm;
绘制矩形笔刷=新绘制();
int g1、g2、g3、g4;
公共bargraphClass(上下文、int-wt、int-ht)
{
超级(上下文);
leftSp=wt/5;
底部SP=ht/20;
startX=leftSp;
startY=0;
stopX=wt;
stopY=ht-bottomSp;
fillRect=新的RectF();
g1=5*60;
g2=4*60;
g3=8*60;
g4=6*60;
}
@凌驾
受保护的void onDraw(画布c)
{
//TODO自动生成的方法存根
内部重量、间隙、温度;
super.onDraw(c);
牵引轴(c);
ppm=(浮动)(停止起动)/(24*60);
_wt=stopX startX;
差距=_wt/9;
温度=(int)(g1*ppm);
fillRect.set(startX+间隙、stopY温度、startX+(间隙*2)、stopY);
rectBrush.setColor(Color.RED);
rectBrush.setAntiAlias(true);
setAlpha(128);
c、 drawRect(fillRect、rectBrush);
温度=(整数)(g2*ppm);
填充直接设置(startX+(间隙*3)、stopY温度、startX+(间隙*4)、stopY);
rectBrush.setColor(Color.GREEN);
rectBrush.setAntiAlias(true);
setAlpha(128);
c、 drawRect(fillRect、rectBrush);
温度=(内部)(g3*ppm);
填充直接设置(startX+(间隙*5)、stopY温度、startX+(间隙*6)、stopY);
rectBrush.setColor(Color.BLUE);
rectBrush.setAntiAlias(true);
setAlpha(128);
c、 drawRect(fillRect、rectBrush);
温度=(整数)(g4*ppm);
填充直接设置(startX+(间隙*7)、stopY温度、startX+(间隙*8)、stopY);
setColor(颜色为洋红色);
rectBrush.setAntiAlias(true);
setAlpha(128);
c、 drawRect(fillRect、rectBrush);
}
私有void setScaleValues()
{
ppm=停车场/(24*60);
}
专用void drawAxis(画布c)
{
int sp;
字符串str;
油漆p=新油漆();
p、 setColor(Color.DKGRAY);
p、 设定行程宽度(2);
p、 setAntiAlias(真);
c、 抽绳(startX、startY、startX、stopY、p);
c、 抽绳(startX、stopY、stopX、stopY、p);
sp=(临时开工)/12;

对于(int i=1;i您是否在片段中使用
SurfaceView
?@User117不确定这是什么意思,刚开始使用图形。我有一个视图类,在其中绘制图形,然后将其添加到布局中。@User117如果有帮助,我已经添加了视图类。