我的自定义视图控件不在片段中重绘

时间:2013-02-15 18:04:36

标签: android view ondraw invalidation

我创建了自定义视图控件,该控件从View控件扩展而来,仅在onDraw方法中绘制了一些信息。

如果我在常规活动布局上创建控件,一切都有效。但是如果我在片段的onCreateView()中创建此控件,它会在创建时绘制,然后我的视图“冻结”。它可以工作并解析,但不会调用onDraw()。我试着打电话给invalidate(),但没有。

有什么想法吗?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)     {       
    final View v = inflater.inflate(R.layout.button, container, false);
    mAgatButton = (agatButton) v.findViewById(R.id.agatButton1);
    mAgatButton.setOnClickListener(new OnClickListener() {      
        @Override
        public void onClick(View view) {
            // This not Repainted           
            final GraphViewSeries TahoSer = mGraphTaho.getSeries((byte)0);
            mGraphTahoPos++;
            TahoSer.appendData(new GraphViewData(mGraphTahoPos, 50d), true);            
            //This Work correct
            mTahoLabel.setText(String.format(getString(R.string.button_rpm), (int)(Math.random()*1000)));

            Log.d(agatButton.class.toString(),"Click Detected: "+TahoSer.size());
        }
    });
    final Context context = getActivity().getApplicationContext();

    LinearLayout graphView = (LinearLayout) v.findViewById(R.id.graph_lay_taho);
    graphView.addView(mGraphTaho = getGraphView(context,"Taho"));

    mTahoLabel = (TextView) v.findViewById(R.id.tahoText);
    mTahoLabel.setText(String.format(getString(R.string.button_rpm), 0));

    return v;
}

private GraphView getGraphView(Context context, String Name)
  {
     final GraphView tmpView = new LineGraphView(context,Name);  
     tmpView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

     List<GraphViewData> initalSeriesData = new ArrayList<GraphViewData>();
     for (byte i=0;i<20;i++)
         initalSeriesData.add(new GraphViewData(i, 0d));

     final GraphViewSeries tahoSeries = new GraphViewSeries(initalSeriesData);

     ((LineGraphView) tmpView).setDrawBackground(true);
     tmpView.addSeries(tahoSeries);
     tmpView.setViewPort(1, 20);
     tmpView.setScalable(false);
     tmpView.setManualYAxis(true);
     tmpView.setManualYAxisBounds(100, 0);

     return tmpView;
  }

如果我不使用Fragment一切正常。有什么问题?

0 个答案:

没有答案