我有一个基于 google 提供的文档代码和 sherlock 操作栏类的选项卡侦听器。
我想让我的两个选项卡使用 ListFragment 类,一个使用自定义 GridFragment 类。
但是,选项卡侦听器一次只能除一种类型,我是否需要创建一个标准片段并在其中放置一个列表视图或网格视图?还是我需要为每个创建一个单独的选项卡侦听器?或者有没有办法让选项卡侦听器除了任何类型的片段、标准、列表或网格,只要它继承自片段类。
package com.NYXDigital.LookBunnyFind;
import android.app.Activity;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
public class TabListener<T extends SherlockFragment> implements ActionBar.TabListener {
private SherlockFragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
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 = (SherlockFragment)SherlockFragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}