1

下面的代码有效,但是当我调用自身时,图表的整个主视图都被替换了。

我想问题出在我如何在 startActivity(chart); 中启动图表 Activity;在我的片段中。如果删除它,该片段无需更换即可正常工作。

我怎么能把它发送到主视图???

public class Graph extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){


    // LineGraph - class which draws the graph using achartengine
    final LineGraph lineGraph = new LineGraph();
    final Intent chart = lineGraph.getIntent(inflater.getContext());

    startActivity(chart);

    return inflater.inflate(R.layout.graph1, container, false);  

}

}

这里的活动:

    public class MainCharts extends Activity {
    Button btn1;

    @TargetApi(11)
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dash_app);

        btn1=(Button)findViewById(R.id.button1);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        StartFragment myFragment = new StartFragment();
        ft.add(R.id.myFragment, myFragment);
        ft.commit();

        btn1.setOnClickListener(btnOnClickListener);

    }

    Button.OnClickListener btnOnClickListener = new Button.OnClickListener(){

        @SuppressLint("NewApi")
        @Override
        public void onClick(View v){

            Fragment newFragment;

            newFragment = new Graph();

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.myFragment, newFragment);
            transaction.addToBackStack(null);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.commit();
        }
    };
}
4

1 回答 1

4

看一下这个示例,了解如何在片段中添加 AChartEngine 图表。

于 2012-10-15T19:42:07.307 回答