2017-02-22 21 views
0

我需要帮助。我正在开发一个应用程序。onItemClick如果转到FirstFragment否则转到SecondFragment

这是我的导航栏。 我有三个选项菜单(彩色的)。

1)FirstFragment
2)MapFragment
3)第三块碎片

Image Here

这是我的抽屉式导航栏包括 “建立类型列表” 中。

点击后会打开另一个片段(名为ListFragment),其中包含该场所的列表。

list

场所必须的功能转到FirstFragment要不然到MapFragment。

场景:

1)如果我在 我FirstFragment->点击导航抽屉>点击ListFragment->银行

在这种情况下,当用户点击该银行将检查用户在FirstFragment中,然后进入FirstFragment。否则它会转到MapFragment ........结果它会转到FirstFragment。

2)如果我在MapFragment-我>点击导航抽屉>点击ListFragment->银行

它检查,如果用户是在FirstFragment否则它会去MapFragment ..... ......结果它将转到MapFragment。

问题:所以这个问题我不知道如何实现它在我的代码...

这里的代码在我的MainActivity:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     android.app.FragmentManager fragmentManager = getFragmentManager(); 

     if (id == R.id.action_ar) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, new FirstFragment()) 
        .commit(); 
      Toast.makeText(this, "First Selected", Toast.LENGTH_SHORT).show(); 
     } 
     else if (id == R.id.action_map) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, new MapFragment()) 
        .commit(); 
      Toast.makeText(this, "MAP Selected", Toast.LENGTH_SHORT).show(); 
     } 
     else if (id == R.id.action_direction) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, new ThirdFragment()) 
        .commit(); 
      Toast.makeText(this, "Third Selected", Toast.LENGTH_SHORT).show(); 
     } 
     return super.onOptionsItemSelected(item); 
    } 
+0

你的问题,所以你需要证明代码... –

+0

问题是我不知道如何。这就是为什么我需要帮助。 – Problematic

+0

我问你用更多的代码请[编辑]你的问题......虽然,你的问题似乎还不清楚。尝试创建一个[mcve] –

回答

-1

可以传递一个变量“from_where_did_icome”(例如)每次你去一个片段或另一个来检查你到达那里。对于使用此代码示例做:

//Passing data to fragment: 

Bundle bun = new Bundle(); 
bun.putString("pathimage", path); 
fragment_setting.setArguments(bun); 

//Getting data in fragment: 

Bundle b = getArguments(); 
String path = b.getString("pathimage"); 

了解更多在这个两个环节:在`FirstFragment`存在 https://developer.android.com/training/basics/fragments/communicating.htmlhttps://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

+0

为什么你需要一个额外的变量来告诉你你在哪个片段?为什么你会使用像php这样的下划线变量名? –

+0

@Nick它不是你所在的位置,而是你从哪个片段/活动中得到的信息,通过这些信息,我们可以决定他用简单的if或者之后的切换来问什么。也编辑了答案。 –

相关问题