想要在从另一个片段接收数据后更新片段视图吗?

时间:2014-03-28 17:13:19

标签: android android-fragments

当我通过界面接收来自另一个片段的回叫时,我正在尝试更新我的饼图视图。所以基本上我想要实现的是当另一个片段将数据发送到我的片段时,我的片段应该相应地更新它的视图。接口方法是RecieveCalorieDistribution。

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

    rootView = inflater.inflate(R.layout.nutrition_goals_for_today, container, false);

    View child=CreatePieChart();
    LinearLayout view=(LinearLayout) rootView.findViewById(R.id.PieChartContainer);
    view.addView(child);


    return rootView;
}
public void RecieveCalorieDistribution(int carbs,int fats,int proteins)
{
    this.carbs=carbs;
    this.fats=fats;
    this.proteins=proteins;
    float total=carbs+fats+proteins;
    this.carbs=(this.carbs/total)*100;
    this.fats=(this.fats/total)*100;
    this.proteins=(this.proteins/total)*100;

}
private View CreatePieChart() {

      // Pie Chart Section Names
      String[] code = new String[] { "Carbohydrates", "Fat","Protein" };

      // Pie Chart Section Value
      double[] distribution = { (int)carbs, (int)fats, (int)proteins};

      // Color of each Pie Chart Sections
      int[] colors = { Color.parseColor("#AFDCEC"),Color.parseColor("#FFF380"), Color.parseColor("#6CBB3C") };//blue,yellow,green

      // Instantiating CategorySeries to plot Pie Chart
      CategorySeries distributionSeries = new CategorySeries(
        "Mobile Platforms");
      for (int i = 0; i < distribution.length; i++) {
       // Adding a slice with its values and name to the Pie Chart
       distributionSeries.add(code[i], distribution[i]);
      }
      // Instantiating a renderer for the Pie Chart
      DefaultRenderer defaultRenderer = new DefaultRenderer();
      for (int i = 0; i < distribution.length; i++) {
       SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
       seriesRenderer.setColor(colors[i]);
       seriesRenderer.setDisplayChartValues(true);
       // Adding a renderer for a slice
       defaultRenderer.addSeriesRenderer(seriesRenderer);
      }
      defaultRenderer.setLegendTextSize(30);
      defaultRenderer.setLegendHeight(45);
      defaultRenderer.setChartTitle("Calories Distribution");
      defaultRenderer.setChartTitleTextSize(20);

      defaultRenderer.setShowLabels(false);
      defaultRenderer.setClickEnabled(false);
      defaultRenderer.setZoomButtonsVisible(false);
      defaultRenderer.setLabelsTextSize(19);
      defaultRenderer.setDisplayValues(true);
      defaultRenderer.setPanEnabled(false);



      View view=ChartFactory.getPieChartView(getActivity(), distributionSeries, defaultRenderer);

      return view;



     }

public class NutritionGoalsFragmentAdapter extends FragmentPagerAdapter {

 private NutritionGoalsForTodayFragment todayFragment;
 private NutritionGoalsSummaryFragment summaryFragment;

public NutritionGoalsFragmentAdapter(FragmentManager fm) {
    super(fm);
    todayFragment=new NutritionGoalsForTodayFragment();
    summaryFragment=new NutritionGoalsSummaryFragment();
}

@Override
public Fragment getItem(int index) {
    switch (index) {
    case 0:
       return todayFragment;


    case 1:
    {

       return summaryFragment;
    }

    }
    return null;
}

@Override
public int getCount() {

    return 2;
}

}

1 个答案:

答案 0 :(得分:0)

好的,我在彻底了解你的评论后找到了解决方案。

public void RecieveCalorieDistribution(int carbs,int fats,int proteins)
{
    this.carbs=carbs;
    this.fats=fats;
    this.proteins=proteins;
    float total=carbs+fats+proteins;
    this.carbs=(this.carbs/total)*100;
    this.fats=(this.fats/total)*100;
    this.proteins=(this.proteins/total)*100;
    View view=CreatePieChart();
    LinearLayout linear=(LinearLayout)rootView.findViewById(R.id.PieChartContainer);
    linear.removeAllViews();
    linear.addView(view);
}