操作栏选项卡:使用多次实例化的单个片段时如何传递参数
Posted
技术标签:
【中文标题】操作栏选项卡:使用多次实例化的单个片段时如何传递参数【英文标题】:Action Bar Tabs: How do I pass arguments when using a single fragment instantiated more than once 【发布时间】:2014-02-25 02:38:15 【问题描述】:我想在我的应用程序中包含操作栏选项卡,并且正在通过一个教程示例来了解这一切。 The tutorial has two tabs each with their own fragment so then when the tab is selected the correct fragment is shown...this seems to be the approach shown in all the examples I can find.
我想要做的是让每个选项卡都有自己的相同片段的实例化,并将参数传递给片段,以便代码可以根据选择的选项卡表现出不同的行为。
这是教程中的代码:
活动(开始活动)
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
appContext = getApplicationContext();
//ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
Fragment PlayerFragment = new AFragment();
Fragment StationsFragment = new BFragment();
PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
StationsTab.setTabListener(new MyTabsListener(StationsFragment));
actionbar.addTab(PlayerTab);
actionbar.addTab(StationsTab);
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch(item.getItemId())
case R.id.menuitem_search:
Toast.makeText(appContext, "search", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_add:
Toast.makeText(appContext, "add", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_share:
Toast.makeText(appContext, "share", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_feedback:
Toast.makeText(appContext, "feedback", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_about:
Toast.makeText(appContext, "about", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_quit:
Toast.makeText(appContext, "quit", Toast.LENGTH_SHORT).show();
return true;
return false;
@Override
protected void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
以下类包含在同一个 java 文件的末尾:
class MyTabsListener implements ActionBar.TabListener
public Fragment fragment;
public MyTabsListener(Fragment fragment)
this.fragment = fragment;
public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft)
Toast.makeText(StartActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft)
ft.replace(R.id.fragment_container, fragment);
public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft)
ft.remove(fragment);
这是一个片段:
public class AFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.afragment, container, false);
这是B片段
public class BFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.bfragment, container, false);
这里是 XML 文件:
Main.xml
<LinearLayout
android:orientation="vertical"
android:id="@+id/fragment_container"
android:layout_
android:layout_ >
片段:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView android:id="@+id/textView1"
android:text="Fragment A"
android:layout_
android:layout_/>
</LinearLayout>
B 片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView android:id="@+id/textView2"
android:text="Fragment B"
android:layout_
android:layout_/>
</LinearLayout>
所以我想做的是完全摆脱 Fragment B 并让 Station 和 Player 选项卡都使用 Fragment A。然后在 Fragment A 中放入代码,如果它是为 Station 做一件事,如果它是另一件事是给玩家的。我已经尝试了各种尝试放置 Args Bundle,但我对此很陌生,我尝试过的任何方法都没有奏效,
【问题讨论】:
【参考方案1】:我现在有一个适用于此的解决方案。 我这样做的方法是在设置每个选项卡时在每个选项卡上设置一个标记,然后在我的片段中获取当前选项卡并使用它的标记。
代码改动如下:
在Activity中,所以我只使用Fragment A:-
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Tab A")
.setTag("A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Tab B")
.setTag("B");
Fragment PlayerFragment = new AFragment();
Fragment StationsFragment = new AFragment();
现在在 Fragment A 中,我已覆盖 onResume() 并输入以下代码:-
@Override
public void onResume()
// TODO Auto-generated method stub
super.onResume();
TextView v = (TextView)getView().findViewById(R.id.textView1);
Tab b = getActivity().getActionBar().getSelectedTab();
String c = b.getTag().toString();
if (c.equals("A"))
v.setText("Tab A has been selected");
if (c.equals("B"))
v.setText("It's Tab B now");
【讨论】:
***.com/questions/67042935/… 告诉我兄弟以上是关于操作栏选项卡:使用多次实例化的单个片段时如何传递参数的主要内容,如果未能解决你的问题,请参考以下文章