1

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

如果我在常规活动布局上创建控件,一切正常。但是,如果我在 Fragment 中创建此控件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 一切正常。怎么了?

4

0 回答 0