有几个片段在他们自己的工作"好?

时间:2016-03-17 19:31:48

标签: android android-fragments

我已经提出了这样的设计,其中一个片段用作菜单,并且无论何时进行选择都会被替换。

public class HistoryFragment extends Fragment implements View.OnClickListener {

private Button buttonLinechart;
private Button buttonPiechart;

public HistoryFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_history, container, false);
    findViewsById(view);
    setListeners();
    return view;
}

private void findViewsById(View view) {
    buttonLinechart = (Button) view.findViewById(R.id.btn_linechart);

}

private void setListeners() {
    buttonLinechart.setOnClickListener(this);
    buttonPiechart.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v == buttonLinechart) {
        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new LineChartFragment()).commit();
    }
}
}

然后新片段访问sqlite数据库并使用getActivity()在其上绘制线图,因此似乎不需要与该活动进行其他通信。

public class LineChartFragment extends Fragment implements View.OnClickListener{

private Button buttonBack;
private LineChart chart;

public LineChartFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_line_chart, container, false);

    findViewsById(view);
    setListeners();

    createLineChartAll();
    return view;
}

private void createLineChartAll() {
    DBHandler dbHandler = new DBHandler(getActivity(), null, null, 1);

    List<String> list = dbHandler.listInr();

    Timber.d("list sample: " + list.get(0));

    String regex = "^(\\d+\\.\\d{1})";
    Pattern p = Pattern.compile(regex);

    ArrayList<Entry> entries = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        Matcher m = p.matcher(list.get(i));
        while (m.find()) {
            Timber.d("regex result: " + m.group());
            entries.add(new Entry(Float.parseFloat(m.group()), i));
        }
    }

    regex = "\\d{4}-\\d{2}-\\d{2}";
    p = Pattern.compile(regex);

    LineDataSet dataSet = new LineDataSet(entries, "INR");

    ArrayList<String> labels = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        Matcher m = p.matcher(list.get(i));
        while (m.find()) {
            Timber.d("regex result: " + m.group());
            labels.add(m.group());
        }
    }

    LineData data = new LineData(labels, dataSet);
    chart.setData(data);
    chart.setDescription("");
}

private void findViewsById(View view) {
    buttonBack = (Button) view.findViewById(R.id.btn_history);
    chart = (LineChart) view.findViewById(R.id.chart);
}

private void setListeners() {
    buttonBack.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v == buttonBack) {
        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new HistoryFragment()).commit();
    }
}
}

一位同学正在持怀疑态度,但如果没有一个好的答案,我想在这里问一下。 这种方式看起来好吗?

干杯

1 个答案:

答案 0 :(得分:1)

拥有多个片段并通过.replace方法让它们进出封闭活动是没问题的。事实上,这是相对标准的。

然而,我对你的代码所说的是,在片段之间进行直接交互并不是一种好的做法。在你的情况下,你有两个片段,每个片段都知道另一个片段。最好的做法是让单个碎片告诉他们关于刚刚发生的事件的封闭活动,并让活动确定要做什么。这使得

变得更容易

一个。在多个活动中重复使用片段

湾配置特定的布局(即景观与肖像,手机与平板电脑)

来自谷歌开发者网站: http://developer.android.com/training/basics/fragments/communicating.html

Often you will want one Fragment to communicate with another, for example to
change the content based on a user event. All Fragment-to-Fragment communication
is done through the associated Activity. Two Fragments should never communicate
directly.

我发现最好在我的片段中定义一个接口,该接口具有一个封闭活动需要实现的回调以容纳片段:

public class MyFragment extends Fragment {
  public interface MyFragmentCallbacks {
     void somethingPushed();
  }

  public void onCreate(Bundle savedInstanceState) {
     ...

     findViewById(R.id.some_button).setOnClickListener(
      new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          ((MyFragmentCallbacks)getActivity()).somethingPushed();
        }
      });
  }
}