繁体   English   中英

如何在LinearLayout上更新片段的视图

[英]How to update a view on LinearLayout for a fragment

我有一个片段GraphFragment,它在图形上使用GraphView绘制我需要的所有数据。 我想为用户提供更改GraphView的网格颜色的选项,但是当我尝试更新视图时,它似乎不起作用(没有任何更改)。 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.

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