使用ActionBarSherlock开发应用程序,我有两个标签。两个选项卡都有listview,如图所示。我需要使用栏上的刷新按钮“刷新”活动选项卡的列表。实现同样目标的最佳方法是什么?
如何从MainActivity调用NetworkInformationUI的notifyDataSetChanged?
以下是代码段:
public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
private FragmentTabHost mTabHost;
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), "Refresh Clicked", Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the view from main_fragment.xml
setContentView(R.layout.main_fragment);
// Locate android.R.id.tabhost in main_fragment.xml
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// Create the tabs in main_fragment.xml
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
// Create Tab1 with a custom image in res folder
mTabHost.addTab(mTabHost.newTabSpec("Network Information").setIndicator("Network Information"),
NetworkInformationUI.class, null);
// Create Tab2
mTabHost.addTab(mTabHost.newTabSpec("Trouble-Shoot N/W").setIndicator("Trouble-Shoot N/W"),
TroubleShootNWUI.class, null);
}
}
答案 0 :(得分:1)
如何从中调用NetworkInformationUI的notifyDataSetChanged MainActivity?
单击刷新菜单项时,使用FragmentManager
获取对标签片段的引用,该标签片段将使用{{1}上设置的标签添加到FragmentTransaction
的布局中例如,(TabHost.TabSpec
代表第一个片段,或者您可以通过"Network Information"
方法使用自己的代码):
TabSpec.setTag()
答案 1 :(得分:0)
这对我有用
FragmentTabHost tabHost3 = (FragmentTabHost) findViewById(android.R.id.tabhost);
String current_tab = tabHost3.getCurrentTabTag();
if (current_tab.equals("abc")) {
ClassFragment ni = (ClassFragment) getSupportFragmentManager().findFragmentByTag("abc");
ni.refresh();
}