【问题标题】:Creating master/detail flow with ViewPager使用 ViewPager 创建主/详细信息流
【发布时间】:2015-06-03 05:38:33
【问题描述】:

如何在选项卡式活动中使用主详细信息流?

我有一个有 3 页的视图寻呼机。我正在尝试使用 android studio 提供的主/详细信息流作为视图寻呼机中的片段之一。

【问题讨论】:

  • 将活动转化为片段?你到底是什么意思?如果您将您的类扩展为诸如 ActionBarActivity 之类的活动,那么它将是一个活动。如果您将其扩展到片段而不是活动,它将表现为片段。
  • 是的,这就是我的意思,但是一旦我实现 Fragment,它会给我带来大量错误,我不知道如何解决它们
  • 我不明白你所说的实现片段是什么意思,如果你发布你的错误和给出错误的部分代码会很有帮助。
  • 我编辑了我的问题,现在清楚了吗?
  • 好的,对你来说,问题是你需要在你的应用程序中使用内置的主从活动作为片段吗?

标签: android android-viewpager android-listfragment master-detail


【解决方案1】:

您可以尝试这样做:

  1. 将扩展从 Activity 更改为 Fragment。
  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 访问 Activity 的任何地方,或者只是用 super.getActivity() 替换的东西。或使用保存在 onCreateView 中的值,如 2) 所示。示例:Intent i = getIntent(); 变为 Intent i = super.getActivity().getIntent()

【讨论】:

  • 感谢摆脱了这些错误,但现在它在运行时给了我这个错误 致命异常:main android.view.InflateException: Binary XML file line #15: Error inflating class fragment
  • 请张贴xml代码。由于这是一个新的错误/问题,如果您将其作为新问题发布,将对您有所帮助。我的回答有帮助,请接受它作为答案并投票。
  • 这正是我想要做的。您的问题解决了吗?
猜你喜欢
  • 2010-12-22
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多