Activity與在Android的碎片Fragment
Android的API 3.0版本,被稱為蜂窩,引入了新的概念,叫做“記憶碎片”。什麼是片斷,為什麼你想使用一個?請繼續閱讀。在Android中,配置的變化(包括將手機側向轉動,以查看屏幕在橫向)導致當前正在運行的活動停止,重載,並使用新的配置參數重新呈現。(我敢肯定,iOS開發者將指出這個“功能”中缺少自己的堆棧)。在這樣做時,您的應用程序將失去所有的狀態信息從最初的活動實例。那豈不是更好,讓解決?輸入片段。片段是,有能力之間保持狀態的配置更改的UI組件(雖然它可能有非UI片段)。片段必須始終與活動相關聯,但一個簡短的電話setRetainInstance(真),與活動相關聯的片段重新連接到新的實例的活動,並保留其原來的狀態。 為了證明片段呆在身邊,我們將創建兩個獨立的片段,每一個選擇時,構建一個隨機ID。然後,我們將兩個片段連接到一個活動。第一個片段最初看到的第二個片段會被隱藏。當你點擊一個按鈕上的第一個片段,它會顯示你的第二,反之亦然。然後,您可以檢查,以確保原片段真的是住周圍的ID,這是一個有用的技術,可以使用較小的設備上顯示UI在一塊。在大型設備,你可以定義一個布局一次顯示兩個片段的。片段允許你構建你 的UI一係列較小的,可重複使用的圖形元素,你安排需要根據設備的能力,讓我們的開始通過創建一個FirstFragment類及SecondFragment類。我假設讀者熟悉與處理onClick事件,所以不會打擾他解釋說。因此,它是你正在尋找在哪個片段,每個片段都會有一個id。
public class FirstFragment extends Fragment { public static final String TAG = "FirstFragmentTag"; int key; public FirstFragment() { this.setRetainInstance(true); Random random = new Random(); key = random.nextInt(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG, "onCreateView"); View view = inflater.inflate(R.layout.first_fragment_layout, container, false); TextView textView = (TextView) view.findViewById(R.id.firstFragmentTextView); textView.setText(textView.getText() + "" + key); Button button = (Button) view.findViewById(R.id.goToSecondFragmentButton); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { SecondFragment secondFragment = (SecondFragment) FirstFragment.this.getActivity() .getSupportFragmentManager().findFragmentById(R.id.secondFragment); secondFragment.getView().setVisibility(View.VISIBLE); FirstFragment.this.getView().setVisibility(View.GONE); } }); return view; } }
ublic class SecondFragment extends Fragment { protected static final String TAG = "SecondFragmentTag"; int key; public SecondFragment() { this.setRetainInstance(true); Random random = new Random(); key = random.nextInt(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG, "onCreateView"); View view = inflater.inflate(R.layout.second_fragment_layout, container, false); TextView textView = (TextView) view.findViewById(R.id.secondFragmentTextView); textView.setText(textView.getText() + "" + key); Button button = (Button) view.findViewById(R.id.goToFirstFragmentButton); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { FirstFragment firstFragment = (FirstFragment) SecondFragment.this.getActivity() .getSupportFragmentManager().findFragmentById(R.id.firstFragment); firstFragment.getView().setVisibility(View.VISIBLE); SecondFragment.this.getView().setVisibility(View.GONE); } }); return view; }
每個片段的布局將包括一個TextView(顯示的ID),一個ImageView(顯示不同的圖形),和一個按鈕(導航到另一個片段)。主要活動布局將包括的LinearLayout,包含的FirstFragment和SecondFragment的。FirstFragment將其可見性設置為“可見”,SecondFragment將有它的可見性設置為“水漲船高”。在這裏簽出文件 。
FirstFragment布局和SecondFragment布局定義每個片段將如何看。
為了證明這些碎片保持其狀態,運行的程序,然後把你的手機,把它變成橫向模式。檢查看看的ID保持不變,為每一個片段後,切換到橫向。他們應該的。然後取出setRetainInstance()調用,然後重新運行該程序。你會發現,IDS改變每次你改變手機的方向。
片段是目前被認為是最好的做法在Android UI設計,很容易明白為什麼,因為他們提供了很大的靈活性。
最後更新:2017-04-02 15:15:00