【问题标题】:Go from Activity to Fragment on NavigationDrawer在 NavigationDrawer 上从 Activity 转到 Fragment
【发布时间】:2015-08-24 13:48:05
【问题描述】:

我有一个类似于演示文稿的 Activity,我想放置一些按钮,以便我可以直接转到特定片段(我从 MainActivity 中选择的片段)。

我已尝试使用 getSupportFragment 和 Activity.getSupportFragment 但没有任何效果。而且,显然,两者都不是出于简单的意图。

谢谢。

这是 MainActivity “Splash”:

  public class Splash extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_splash);


    }
}

FragmentActivity 的开始。它不完整,因此包含错误。这是包含 Fragments 的 Activity

public class FragmentActivity extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

要在视图之间切换,我使用:

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment views=null;
    switch (position) {
        case 0:
            //news
            views= new news();
            break;
        case 1:
            //time
            views=new time();
            break;
        case 2:
            //Ins
            views=new Ins() ;
            break;

    }

fragmentManager.beginTransaction()
            .replace(R.id.container, views)
            .commit();
}

【问题讨论】:

  • 请附上一些现有的代码
  • 我不明白:你需要什么?片段在哪里?请放一个屏幕或一些代码:)
  • 已编辑。抱歉耽搁了
  • Np,我还有一个问题:如果我明白了,您有一个包含 3 个(或更多)片段的页面,并且根据单击的按钮,您想让特定片段可见,其他片段隐藏对吗?
  • 是的,我有一个我想像“预菜单”一样的活动。在它(Splash Activity)中,我想放置一些引用每个片段的按钮。所以,如果我按下名为“时间”的按钮,我会显示显示时间视图的片段活动。

标签: android android-activity fragment navigation-drawer


【解决方案1】:

好的,我可以尝试给您一个解决方案:您可以按照this 指南将所有片段添加到 SplashActivity,而不是创建新视图。

将所有片段设置为View.GONE。 用户单击按钮后,将相关片段的 View 属性更改为Visible,将所有其他片段的属性更改为Gone

每次用户单击按钮时,所有片段都会消失,除了您需要的片段。

这应该可以,但我建议查看official documentationthis 指南,了解如何正确使用 NavigationDrawer。

希望我有所帮助,祝你好运!

【讨论】:

  • 谢谢,我会带着指南和文档去
  • 没问题,这是我找到的第一个指南,但是网络已满,请选择您喜欢的指南! :)
猜你喜欢
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 2018-04-14
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多