使用ViewPager创建主/详细信息流

时间:2015-03-30 09:08:31

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

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

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

1 个答案:

答案 0 :(得分:2)

您可以尝试这样做:

  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()替换的内容。或者使用onCreateView中保存的值,如2)所示。示例:Intent i = getIntent();变为Intent i = super.getActivity().getIntent()