我有4个Fragments
,一个导航片段有3个按钮和3个画布片段。由于我需要计算layout
我Fragment
使用onfocuschangedlistener
的{{1}}的大小来绘制canvas
。
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);
}
});
这在首次加载应用程序时工作正常,我默认加载视图,但在此之后,一旦我开始通过Navigation片段切换片段,视图永远不会重绘。我发现即使我加载了另一个onglobalfocuschange
,也永远不会再调用Fragment
。我怎样才能做到这一点。感谢任何建议。
这是我的View类绘制条形图。现在我只是使用常数值进行测试。
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);
}
}
}