【问题标题】:Android Master Detail template and ActionBarAndroid Master Detail 模板和 ActionBar
【发布时间】:2015-02-19 10:59:13
【问题描述】:

我是 Android 新手..

我有一个关于 MasterDetail 模板的问题。

我有一个活动来管理用户登录。当登录数据正确时,我需要显示 Master Detail 模板。

所以,在 Android Studio 中,我在我的项目中添加了 Master/Detail Flow 活动。

在我的validateLogin() 方法中,登录数据正确后,我必须在主/明细流中显示Activitys,我使用此代码:

Intent myIntent = new Intent(Login.this, MasterDetailListActivity.class);
startActivity(myIntent);
finish();

如果此代码正确,我不安全,我还想显示 actionBar(实际上我只能在 detailView 中看到 ActionBar)。

******编辑********

这是我的列表活动的代码

public class BusinessPartnerListActivity extends FragmentActivity
        implements BusinessPartnerListFragment.Callbacks {

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_businesspartner_list);

        if (findViewById(R.id.businesspartner_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;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            ((BusinessPartnerListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.businesspartner_list))
                    .setActivateOnItemClick(true);
        }

        // TODO: If exposing deep links into your app, handle intents here.
    }

    /**
     * Callback method from {@link BusinessPartnerListFragment.Callbacks}
     * indicating that the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(String id) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(BusinessPartnerDetailFragment.ARG_ITEM_ID, id);
            BusinessPartnerDetailFragment fragment = new BusinessPartnerDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.businesspartner_detail_container, fragment)
                    .commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, BusinessPartnerDetailActivity.class);
            detailIntent.putExtra(BusinessPartnerDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }
}

...以及详细活动...

public class BusinessPartnerDetailActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_businesspartner_detail);

        // Show the Up button in the action bar.
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // savedInstanceState is non-null when there is fragment state
        // saved from previous configurations of this activity
        // (e.g. when rotating the screen from portrait to landscape).
        // In this case, the fragment will automatically be re-added
        // to its container so we don't need to manually add it.
        // For more information, see the Fragments API guide at:
        //
        // http://developer.android.com/guide/components/fragments.html
        //
        if (savedInstanceState == null) {
            // Create the detail fragment and add it to the activity
            // using a fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(BusinessPartnerDetailFragment.ARG_ITEM_ID,
                    getIntent().getStringExtra(BusinessPartnerDetailFragment.ARG_ITEM_ID));
            BusinessPartnerDetailFragment fragment = new BusinessPartnerDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.businesspartner_detail_container, fragment)
                    .commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpTo(this, new Intent(this, BusinessPartnerListActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

***** 编辑(视频)****** ActionBar 动画问题视频

https://www.dropbox.com/s/af07ovbv36pn44x/actionbab_issue.mov?dl=0

【问题讨论】:

  • 你不应该打电话给finish()
  • @JonasCz 好的,如何在列表(主)活动中显示 ActionBar? (实际上我只能在 detailView 中看到 ActionBar)。

标签: android android-layout android-fragments android-activity android-actionbar


【解决方案1】:

您需要从ActionBarActivity 扩展而不是FragmentActivity。将您的列表视图活动定义更改为:

public class BusinessPartnerListActivity extends ActionBarActivity
    implements BusinessPartnerListFragment.Callbacks {


    // rest of the code remains the same ...
    // ...
    // ...

}

试试这个。这将起作用。

【讨论】:

  • 好的,现在我可以在列表活动中看到 actionBar... 但是,为什么我在查看详细信息之前选择列表中的项目时会看到奇怪的动画? ActionBar 不保持固定(仅更改内容(主-> 细节)...我看到一个快速动画:好像 ActionBar 在显示细节之前略微下降。你能告诉我这是为什么吗?你可以避免这个?
  • @Safari:您能否提供一张图片或视频,说明您选择列表项时究竟发生了什么?另请提供您的代码以了解详细活动
  • @Zygotelnit 我编辑了问题:我添加了Action bar动画的代码和视频。
  • @Safari:这似乎是新棒棒糖动画过渡的问题。看看this 的帖子,可能有用:)
【解决方案2】:

是的,你做得对,我的意思是你的代码是完美的。如果你想完成你的当前然后

finish();

在其他方面是完美的,无需调用它。

【讨论】:

  • 好的,谢谢... 我怎样才能在列表(主)活动中显示 ActionBar? (实际上我只能在 detailView 中看到 ActionBar)。
  • @ZygoteInit 我用列表活动的代码编辑了我的问题
猜你喜欢
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多