[英]How to update a view on LinearLayout for a fragment
I have a fragment , GraphFragment which plots all the data I need using GraphView on a graph. 我有一个片段GraphFragment,它在图形上使用GraphView绘制我需要的所有数据。 I want to give the user the option to change the gridcolor of the GraphView but when I try to update the view it doesn't seem to work (nothing changes). 我想为用户提供更改GraphView的网格颜色的选项,但是当我尝试更新视图时,它似乎不起作用(没有任何更改)。 The setGridColor function relised on the specific listview index the user presses. setGridColor函数依赖于用户按下的特定列表视图索引。
public class GraphFragment extends Fragment {
private GraphView graphView;
LinearLayout layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.graph_fragment, container, false);
//Initialise the glucose level graph
graphView = new LineGraphView(this.getActivity(), "Blood Glucose (mmol/l)");
graphView.getGraphViewStyle().setVerticalLabelsWidth(40);
//Add the test data
graphView.addSeries(testSeries);
layout = (LinearLayout) rootView.findViewById(R.id.graph1);
layout.addView(graphView);
}
public void setGridColor (int position){
if(position==0){
//graphView = new LineGraphView(this.getActivity(), "Blood Glucose (mmol/l)");
layout.removeView(graphView);
graphView.getGraphViewStyle().setGridColor(Color.RED);
layout.addView(graphView);
}
if(position==1){
//graphView = new LineGraphView(this.getActivity(), "Blood Glucose (mmol/l)");
layout.removeView(graphView);
graphView.getGraphViewStyle().setGridColor(Color.BLUE);
layout.addView(graphView);
}
if(position==2){
//graphView = new LineGraphView(this.getActivity(), "Blood Glucose (mmol/l)");
layout.removeView(graphView);
graphView.getGraphViewStyle().setGridColor(Color.GREEN);
layout.addView(graphView);
}
}
}
更改后,尝试运行graphView.invalidate()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.