Android 要在从另一个片段接收数据后更新片段视图吗?
当我通过接口收到来自另一个片段的回调时,我正在尝试更新饼图视图。所以基本上我想要实现的是,当另一个片段将数据发送到我的片段时,我的片段应该相应地更新它的视图。接口方法为接收热量分配Android 要在从另一个片段接收数据后更新片段视图吗?,android,android-fragments,Android,Android Fragments,当我通过接口收到来自另一个片段的回调时,我正在尝试更新饼图视图。所以基本上我想要实现的是,当另一个片段将数据发送到我的片段时,我的片段应该相应地更新它的视图。接口方法为接收热量分配 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout
@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;
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
rootView=充气机。充气(R.layout.nutrition\u goals\u for\u today,container,false);
查看子对象=CreatePieChart();
LinearLayout视图=(LinearLayout)rootView.findViewById(R.id.PieChartContainer);
view.addView(子视图);
返回rootView;
}
公共空间接受热量分配(int碳水化合物、int脂肪、int蛋白质)
{
这个。碳水化合物=碳水化合物;
这个。脂肪=脂肪;
这个。蛋白质=蛋白质;
浮动总量=碳水化合物+脂肪+蛋白质;
这个.carbs=(这个.carbs/总数)*100;
this.fats=(this.fats/总数)*100;
this.proteins=(this.proteins/总数)*100;
}
私有视图CreatePieChart(){
//饼图分区名称
字符串[]代码=新字符串[]{“碳水化合物”、“脂肪”、“蛋白质”};
//饼图截面值
双[]分布={(int)碳水化合物,(int)脂肪,(int)蛋白质};
//每个饼图部分的颜色
int[]colors={Color.parseColor(“#AFDCEC”)、Color.parseColor(“#FFF380”)、Color.parseColor(“#6CBB3C”)};//蓝色、黄色、绿色
//实例化类别以绘制饼图
CategorySeries distributionSeries=新的CategorySeries(
“移动平台”);
对于(int i=0;i
}好的,在完全理解您的评论后,我找到了解决方案
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);
}
你在哪里使用碎片?饼图不只是添加到布局中的视图吗?如果布局中有一个片段,可以在中加载饼图视图。然后,当新数据出现时,您可以将片段换成新的片段。是的,当接口从NutritionOALSSummaryFragment调用时,我想制作一个新的饼图,我该怎么做?我不确定这是否是最好的方法,但这里有一个想法:假设ReceiveCarriedDistribution是使用新数据调用的函数。创建一个新的饼图视图。然后找到piechart容器并调用view.removeAllViews(),然后只添加新的视图,您的解决方案会起作用吗?在onCreateView()中调用view.removeAllViews()?我是如何找到piechart容器的?对不起,我不是这里的专家。你能用代码的形式给出一个答案,这样我就可以正确地理解你了。是的,这有用吗?我不确定您是否可以在onCreate之外使用findViewById。我想你们可能必须在main中创建一个变量。是的,我必须在这个片段的类级别创建一个变量,是的,它正在工作。