您好我在操作栏和使用ABS的标签时遇到问题。我有两个标签,第一个,"收藏夹",只是扩展Fragment
并且没有显示任何内容。第二个," Lines",扩展SherlockListFragment
并实现LoaderManager.LoaderCallbacks
(从数据库加载用户列表)。
应用程序运行正常(或至少,我认为是这样),但是当我旋转屏幕时会出现问题。如果我处于纵向模式并且选择了LinesTab(显示列表)然后更改为横向,则FavouritesTab(最初为空)的内容将填充LinesTab的内容。
这是代码的重要部分。
Main.java
(创建两个标签页)
public class Main extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
Tab tab = actionBar
.newTab()
.setText("Favourites")
.setTabListener(new FavouritesTab());
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("Lines")
.setTabListener(new LinesTab());
actionBar.addTab(tab);
}
}
LinesTab.java
(使用LoaderManager从数据库加载用户的选项卡)
public class LinesTab extends SherlockListFragment implements ActionBar.TabListener,
LoaderManager.LoaderCallbacks<ArrayList<User>> {
UserAdapter adapter;
Database db;
public void onCreate(Bundle savedInstanceState) {
adapter = new UserAdapter(getActivity(), new ArrayList<User>());
setListAdapter(adapter);
getActivity().getSupportLoaderManager().initLoader(0, null, this);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
return super.onCreateView(inflater, container, state);
}
@Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
}
// TabListener callbacks
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.add(android.R.id.content, this, "LinesTabTag");
ft.attach(this);
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.detach(this);
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {}
// LoaderCallbacks
@Override
public Loader<ArrayList<User>> onCreateLoader(int id, Bundle args) {
db = new Database(getActivity());
return new UserLoader(getActivity(), db);
}
@Override
public void onLoadFinished(Loader<ArrayList<User>> loader,
ArrayList<User> usuarios) {
((UserAdapter) getListAdapter()).update(usuarios);
((UserAdapter) getListAdapter()).notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader<ArrayList<User>> loader) {
setListAdapter(null);
}
}
FavouritesTab.java
(另一个标签,没有显示任何内容)
public class FavouritesTab extends Fragment implements ActionBar.TabListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
return super.onCreateView(inflater, container, state);
}
// TabListener callbacks
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.add(android.R.id.content, this, "LinesTabTag");
ft.attach(this);
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.detach(this);
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {}
}
此外,我不太确定是否所有代码都以正确的方式实现。特别是onTabSelected
方法。在Android documentation中,它们实现了一个管理所有片段的监听器,但我在每个片段中实现了一个监听器。所以我不知道ft.add
然后ft.attach
是否是实施该方法的最佳方式。
感谢。
答案 0 :(得分:3)
最后我找到了解决方案。我在TabListener
中实现了一般Main.java
并在每次旋转屏幕时保存当前标签索引:
Main.java
public class Main extends SherlockFragmentActivity {
int pos = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
if (savedInstanceState != null) {
// Restore the last tab position
pos = savedInstanceState.getInt("pos");
}
Tab tab = actionBar
.newTab()
.setText("Favourites")
.setTabListener(new TabListener<FavouritesTab>(this, "Favourites", FavouritesTab.class));
//.setTabListener(new FavouritesTab());
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("Lines")
.setTabListener(new TabListener<LinesTab>(this, "LinesTab", LinesTab.class));
//.setTabListener(new LinesTab());
actionBar.addTab(tab);
// Set the last tab position, or zero by default
getSupportActionBar().setSelectedNavigationItem(pos);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// Save the current tab position
super.onSaveInstanceState(outState);
outState.putInt("pos", getSupportActionBar().getSelectedNavigationIndex());
}
// The TabListener
static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private SherlockFragmentActivity mActivity;
private String mTag;
private Class<T> mClass;
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it on order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
}
}
我已删除LinesTab.java
和FavouritesTab.java