2

嗨,我在使用 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 文档中,他们实现了一个管理所有片段的侦听器,但我在每个片段中都实现了一个侦听器。所以我不知道是否ft.add和 thenft.attach是实现该方法的最佳方式。

谢谢。

4

1 回答 1

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.javaFavouritesTab.java

于 2013-07-14T17:00:29.497 回答