2016-03-17 53 views
1

我已經想出了這種設計,其中一個片段用作菜單,並且在做出選擇時被替換。有幾個片段「獨立」工作好嗎?

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

似乎沒什麼問題:) –

回答

1

它的優良有多個片段,並讓他們通過.replace方法進來,走出封閉的活動。其實這是比較標準的。

但是,我想說的是你的代碼是不被認爲是在片段之間進行直接交互的好習慣。就你而言,你有兩個片段,每個都知道另一個片段。最好的做法是讓單個片段告訴他們關於剛剛發生的事件的封閉活動,並讓活動確定要做什麼。這使得它更容易爲

a。在多個活動中重複使用片段

b。配置的具體佈局

右鍵,從谷歌的開發者網站(即景觀VS波泰特,手機VS平板電腦。): 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(); 
     } 
     }); 
    } 
} 
+0

所以在我的情況,我應該有活動手柄替代交易,但電子其餘通風口是好的,因爲他們不需要知道其他碎片?感謝回覆! – ludolover

+0

是的,該活動應處理任何片段替換或轉換到其他活動。 – Matt