简体   繁体   English

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

[英]Want to update fragment view after receiving data from another fragment?

I am trying to update my pie chart view when i recieve a call back from another fragment through an interface. 当我通过接口收到来自另一个片段的回叫时,我正在尝试更新饼图视图。 So basically what I am trying to achieve is that when the other fragment sends over the data to my fragment, the my fragment should update it's view accordingly. 因此,基本上我想实现的是,当另一个片段将数据发送到我的片段时,我的片段应相应地更新其视图。 The interface method is RecieveCalorieDistribution. 接口方法是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;
}

} }

Okay I have found the solution after thoroughly understanding your comment. 好吧,在完全理解您的评论之后,我已经找到了解决方案。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM