簡體   English   中英

使用ViewPager創建主/明細流

[英]Creating master/detail flow with ViewPager

如何在選項卡式活動中使用主細節流程?

我有一個具有3頁的查看尋呼機。 我正在嘗試使用android studio提供的主/詳細信息流作為視圖分頁器中的片段之一。

您可以嘗試這樣做:

  1. 將擴展從“活動”更改為“片段”。
  2. 添加方法onCreateView並將所有內容從onCreate移到那里,除了super.onCreate()setContentView():

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { FragmentActivity faActivity = (FragmentActivity) super.getActivity(); // Replace LinearLayout by the type of the root element of the layout you're trying to load LinearLayout llLayout = (LinearLayout) inflater.inflate(R.layout.activity_layout, container, false); // Of course you will want to faActivity and llLayout in the class and not this method to access them in the rest of // the class, just initialize them here 

    //先前onCreate()的內容// //

     // Don't use this method, it's handled by inflater.inflate() above : // setContentView(R.layout.activity_layout); // The FragmentActivity doesn't contain the layout directly so we must use our instance of LinearLayout : llLayout.findViewById(R.id.someGuiElement); // Instead of : // findViewById(R.id.someGuiElement); return llLayout; // We must return the loaded Layout 

    }

  3. 刪除方法onCreate。

  4. 您可以通過this.something或僅用super.getActivity()替換super.getActivity()地方訪問活動。 或使用保存在onCreateView中的值,如2)所示。 示例: Intent i = getIntent(); 成為Intent i = super.getActivity().getIntent()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM