如何使用achartengine为片段中的Intent充气(How to inflate an Intent in a fragment using achartengine)
下面的代码可以工作,但是当我调用自己时,整个主视图将替换为图表。
我想这个问题在于如何在startActivity(图表)中启动图表Activity; 在我的framgment中。 如果删除它,片段可以正常工作而无需替换。
我怎么能把它发送到主视图???
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(); } }; }The code below works, but the whole main view is replaced for the chart when I invoke itself.
I guess that the problem is located in how I start the chart Activity in startActivity(chart); in my framgment. If remove it, the fragment works well without replacement.
How could I send it to the main view???
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); } }Here the Activity:
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(); } }; }最满意答案
看一下如何在片段中添加AChartEngine图表的示例。
Take a look at this for an example on how to add an AChartEngine chart inside a fragment.
更多推荐
发布评论