简体   繁体   English

使用ViewPager创建主/明细流

[英]Creating master/detail flow with ViewPager

How can I use the master detail flow inside a tabbed activity? 如何在选项卡式活动中使用主细节流程?

I have a view pager that has 3 pages. 我有一个具有3页的查看寻呼机。 I'm trying to use the master/detail flow provided by android studio as one of the fragments in the view pager. 我正在尝试使用android studio提供的主/详细信息流作为视图分页器中的片段之一。

You can try doing this: 您可以尝试这样做:

  1. Change the extends from Activity to Fragment. 将扩展从“活动”更改为“片段”。
  2. Add the method onCreateView and move everything from onCreate there except super.onCreate() and setContentView(): 添加方法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 

    // Content of previous onCreate() 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. Remove method onCreate. 删除方法onCreate。

  4. Everywhere you access the Activity with this.something or just something replace with super.getActivity() . 您可以通过this.something或仅用super.getActivity()替换super.getActivity()地方访问活动。 or use the value saved in the onCreateView like shown in 2). 或使用保存在onCreateView中的值,如2)所示。 Example : Intent i = getIntent(); 示例: Intent i = getIntent(); become Intent i = super.getActivity().getIntent() 成为Intent i = super.getActivity().getIntent()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM