使用 ActionBarSherlock 开发应用程序,我有两个相同的选项卡。如图所示,两个选项卡都有 ListView 。我需要使用栏上的刷新按钮“刷新” Activity 选项卡的列表。实现相同目标的最佳方法是什么?
如何在MainActivity中调用NetworkInformationUI的notifyDataSetChanged?
下面是代码 fragment :
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);
}
}
最佳答案
How to call notifyDataSetChanged of NetworkInformationUI from MainActivity?
当您单击刷新菜单项时,使用 FragmentManager
获取对选项卡 fragment 的引用,这些 fragment 将使用标记集添加到 FragmentTransaction
中的布局在 TabHost.TabSpec
(例如,第一个 fragment 的 "Network Information"
上,或者您可以通过 TabSpec.setTag()< 使用您自己的标签
方法):
int currentTab = mTabHost.getCurrentTab();
if (currentTab == 0) { // first tab is currently selected
NetworkInformationUI ni = (NetworkInformationUI) getSupportFragmentManager().findFragmentByTag("Network Information");
// update the list
} else {
TroubleShootNWUI tnui = (TroubleShootNWUI) getSupportFragmentManager().findFragmentByTag("Trouble-Shoot N/W");
// update the list
}
关于android - 如何刷新 SherlockFragmentActivity 的 FragmentTabHost 中的 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19663448/