經過一週的時間,使用實現像TabActivity和fragments和viewPager這樣的選項卡的常見方式,我無法獲得活動或選項卡片段的實例,也無法找到解決問題的方法。所以我決定以不同的方式實施它。首先我用一個簡單的按鈕製作一個tabWidget。在android開發人員網站上,我找到了一種在運行時替換片段的方法。所以唯一剩下的就是如何訪問Tab碎片來調用我的FragmentActivity中的方法。實現帶有簡單視圖和片段的選項卡
這是我使用pageViewer的FragmentActivity。我得到空指針,當我想找到自己的標籤的片段對象setup()
方法:
public class MainActivity extends FragmentActivity implements
OnTabChangeListener, OnPageChangeListener {
private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>();
private PagerAdapter mPagerAdapter;
private TabInfo mLastTab = null;
private class TabInfo {
private String tag;
private Class clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
class TabFactory implements TabContentFactory {
private final Context mContext;
/**
* @param context
*/
public TabFactory(Context context) {
mContext = context;
}
/**
* (non-Javadoc)
*
* @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.d("checking", "setup tabs...");
setContentView(R.layout.activity_main);
// //
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
intialiseViewPager();
// //
setup();
// ///
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
// selected
super.onSaveInstanceState(outState);
}
private void intialiseViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments
.add(Fragment.instantiate(this, CoachFragment.class.getName()));
fragments
.add(Fragment.instantiate(this, LogingFragment.class.getName()));
fragments.add(Fragment.instantiate(this,
HistoryFragment.class.getName()));
this.mPagerAdapter = new PagerAdapter(
super.getSupportFragmentManager(), fragments);
//
this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
mViewPager.setOffscreenPageLimit(1000000);
}
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
TabInfo tabInfo = null;
View tabView1 = createTabView(this, coach");
MainActivity.AddTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab1").setIndicator(tabView1),
(tabInfo = new TabInfo("Tab1", CoachFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
View tabView2 = createTabView(this, logbook");
MainActivity.AddTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab2").setIndicator(tabView2),
(tabInfo = new TabInfo("Tab2", LogingFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
View tabView3 = createTabView(this, "history");
MainActivity.AddTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Tab3").setIndicator(tabView3),
(tabInfo = new TabInfo("Tab3", HistoryFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
mTabHost.setOnTabChangedListener(this);
}
private static void AddTab(MainActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}
public void setup() {
....
CoachFragment fragment=(CoachFragment) mPagerAdapter.getRegisteredFragment(mViewPager.getCurrentItem());
fragment.setTempView(R.id.welcome);
fragment.animate();
....
}
public void onTabChanged(String tag) {
// TabInfo newTab = this.mapTabInfo.get(tag);
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
this.mTabHost.setCurrentTab(position);
}
}
我pageAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
private HashMap<Integer, Fragment> registeredFragments=new HashMap<Integer, Fragment>();
/**
* @param fm
* @param fragments
*/
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
}
和我LogingFragment。其他2個片段是完全一樣的一個:
public class LogingFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout)inflater.inflate(R.layout.coach_activity, container, false);
}
}
每一件事情完美的作品,所有的前瞻性和刷卡工作正常,但在設置()方法,我得到空指針。請幫助我。
我使用的API級別10,所以我不認爲我能夠使用ActionBar選項卡。 –
在這種情況下,我建議你認真考慮使用操作欄sherlock。 – IanB
@Mohamad Ghadourian ^^是的,我忘了那件事。動作欄sherlock爲較低的api級別提供了動作欄的支持。 – scottyseus