如何在其他片段中的值发生更改时更新片段

时间:2014-04-24 21:27:57

标签: android android-fragments

我试图在片段中绘制一个图形,其中包含我在另一个片段中计算的值。 另一个片段是更改类(AnalysedTweet)中的静态变量,这些变量在片段中用于绘制图形。

有没有办法更新图表? (用图表更新片段)

这是图片片段的代码:

public class Statistics extends Fragment {

    GraphicalView mChartView= null;
    private DefaultRenderer mRenderer = new DefaultRenderer();
    private CategorySeries mSeries = new CategorySeries("Sentiment");
    private int[] COLORS = new int[] {Color.parseColor("#009900"),Color.RED, Color.GRAY};

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.activity_statistics,  container, false);
        int[] count = new int[3];
        count[0] = AnalysedTweet.getCount_pos();
        count[1] = AnalysedTweet.getCount_neg();
        count[2] = AnalysedTweet.getCount_obj();

        String[] categoryNames = {"Positive","Negative","Objective" };
        for(int i=0;i<3;i++){

        mSeries.add(categoryNames[i], count[i]);
        SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
        renderer.setColor(COLORS[i]);
        renderer.setDisplayChartValues(true);
        mRenderer.addSeriesRenderer(renderer);

        }


        mChartView = ChartFactory.getPieChartView(this.getActivity(), mSeries, mRenderer);

        Log.i("CHART", Integer.toString(mSeries.getItemCount()));
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        mChartView.setLayoutParams(params);
        RelativeLayout layout = (RelativeLayout) rootView.findViewById(R.id.chartsRelativeLayout);
        layout.addView(mChartView);

        return rootView;
    }


    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

首先,你永远不应该使用静态变量(也就是全局变量)进行交流,这真的是不好的做法。这是谷歌就此事提出建议的方式:

  

通常你会想要一个片段与另一个片段进行通信   示例根据用户事件更改内容。所有   片段到片段的通信是通过相关联的   活动。两个碎片永远不应该直接通信。   要允许片段与其活动进行通信,您可以定义   Fragment类中的一个接口,并在其中实现它   活动。 Fragment捕获了接口实现   它的onAttach()生命周期方法然后可以调用接口   方法,以便与活动进行沟通。

     

以下是片段到活动通信的示例:

public class HeadlinesFragment扩展ListFragment {         OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    ...
}

查看更多heree http://developer.android.com/training/basics/fragments/communicating.html