我有一个ActionBarActivity,它实现了ActionBar.TabListener,如下所示
public class MainActivity extends ActionBarActivity implements
ActionBar.TabListener {
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
Tab tab1 = actionBar.newTab();
tab1.setText("Home");
tab1.setTabListener(this);
actionBar.addTab(tab1);
//我还有其他几个标签 }
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg0);
}
我有一些像下面那样的ListFragment。
public class Home extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
return rootView;
}
}
我需要根据特定条件将某些键或值从MainActivity发送到ListFragment 。我如何实现这一目标?我如何从ListFragment中接收这些值?我也在使用ViewPager。所以我无法传递静态片段名称。
答案 0 :(得分:1)
你可以这样做 -
MainActivity类 -
public class MainActivity extends ActionBarActivity implements
ActionBar.TabListener {
private String myString = "hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
...
}
public String getMyData() {
return myString;
}
}
Home Fragment class -
public class Home extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
MainActivity activity = (MainActivity) getActivity();
String myDataFromActivity = activity.getMyData();
return rootView;
}
}