根据设备大小在主详细信息流中创建自定义列表视图

时间:2014-05-21 09:26:37

标签: android android-fragments android-listview

Master Detail流程的基本模板使用boolean mTwoPane值来确定是否有两个窗格或一个窗格。但是,它是在ListFragment呈现之后设置的。我想为小型设备创建一个ListView,这些设备会在不同的活动中显示DetailFragment,在设备上显示不同的ListView,这会将DetailFragment并排呈现给ListFragment

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_item_list);

    if (findViewById(R.id.item_detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-large and
        // res/values-sw600dp). If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;
        // rest of code
    }

我在确定setContentView(R.layout.activity_item_list)后尝试使用mTwoPane,但mTwoPane始终保持为假。我也尝试过使用setContentView(R.layout.activity_item_list)两次,在if块之前和之后,但应用程序崩溃了。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在您的布局中,只有在FrameLayoutFragment添加if block后才能使用Fragment(而非FrameLayout)。

删除xml中的“fragment”标记,以便完全控制Fragment管理。