【问题标题】:Keep my Navigation Drawer when starting a Fragment Activity开始片段活动时保留我的导航抽屉
【发布时间】:2016-12-25 00:06:25
【问题描述】:

到目前为止,我有一个 MainActivity 可以通过 Fragments 进行交换。但是我已经实现了FragmentActivity,以便在我的抽屉部分中使用ViewPager 并通过Fragments 进行交换。

但是当我点击创建ViewPager的抽屉项目时,抽屉会一起消失。

这是我的FragmentActivity

private Fragment checkFragment(int itemId) { // Checks what item the user has pressed and calls the right Fragment / Activity
    Fragment fragment = null;

    switch (itemId) {
        // HOME (If user clicks on HOME in Drawer)
        case R.id.nav_home:         
            fragment = new Home(); // Set Fragment to be used
            break;
        // STATS GENERAL (If user clicks on General Stats)
        case R.id.nav_stat_general:
            startActivity(new Intent(this, GeneralStatistics.class)); // Start the Fragment Activity
            fragment = null;
            break;     
    }
    return fragment;
}

这是我的Fragment Activity onCreate 方法:

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

    mStatisticsPagerAdapter = new StatisticsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mStatisticsPagerAdapter);
}

即使我从一个 Activity 转到另一个 Activity,我如何才能保留我的 Drawer?

【问题讨论】:

  • 我可以让您将 GeneralStatistics 类的范围更改为片段而不是活动吗?
  • 我试过了,改变了我在第一个代码 sn-p 中调用片段的方式,但抽屉不显示
  • sn-p 是什么意思?
  • 抽屉应该按片段随意工作,你会添加你的抽屉代码和第一个片段代码

标签: android android-fragments


【解决方案1】:

保留您的导航抽屉。您必须用另一个 Fragment 替换 viewpager 内容。

来自您的代码

startActivity(new Intent(this, GeneralStatistics.class));

这一行清楚地表明您打算进行另一个活动(而不是替换您的浏览器内容的片段)

我建议你看看这个Example

【讨论】:

    【解决方案2】:

    解决/解决我的问题:

    我不再使用ActivityFragment,而只是使用Fragment,并将其onCreate 替换为以下内容:

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
    
        view =  inflater.inflate(R.layout.statistics_general_vp, container, false);
        // No more setContentView
    
        mViewPager = (ViewPager) view.findViewById(R.id.pager);
        mStatisticsPagerAdapter = new StatisticsPagerAdapter(getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(mStatisticsPagerAdapter);
    
        return view;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      相关资源
      最近更新 更多