2013-07-22 94 views
0

經過一週的時間,使用實現像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); 
} 

}

每一件事情完美的作品,所有的前瞻性和刷卡工作正常,但在設置()方法,我得到空指針。請幫助我。

回答

1

最後我想通了什麼是空指針的原因。尋呼機適配器應該是這樣的:像下面

public class PagerAdapter extends FragmentPagerAdapter { 

Fragment screens[]; 
public PagerAdapter(FragmentManager fm, MainActivity context) { 
    super(fm); 
    screens = new Fragment[3]; 
    screens[0] = Fragment.instantiate(context,CoachFragment.class.getName()); 
    screens[1] = Fragment.instantiate(context,LogingFragment.class.getName()); 
    screens[2] = Fragment.instantiate(context,HistoryFragment.class.getName()); 
} 

@Override 
public Fragment getItem(int index) { 
    if(index <= screens.length) 
    { 
     return screens[index]; 
    } 
    return null; 
} 

@Override 
public int getCount() { 
    return screens.length; 
} 

}

所以實例化視圖尋呼機變化:

private void intialiseViewPager() { 


    this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),this); 
    // 
    this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager); 
    this.mViewPager.setAdapter(this.mPagerAdapter); 
    this.mViewPager.setOnPageChangeListener(this); 
    // 
    this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager); 
    this.mViewPager.setAdapter(this.mPagerAdapter); 
    this.mViewPager.setOnPageChangeListener(this); 
} 

由於@Bruno馬特烏斯

0

喜歡你是新到Android在我看來,這樣讓我指出你在正確的方向(如果你是不是新的機器人,我道歉):

如果你想在你的應用程序使用的標籤,最新和最好的方法是使用Action Bar tabs

這隻涉及編輯您的menu.xml文件並將onCreateOptionsMenu()方法添加到您的主要活動。

另外,回答你的問題,

所以唯一的事情仍然是如何獲得訪問標籤碎片從我FragmentActivity調用方法

你不應該用你的片段從你的片段活動中調用方法。

同樣,我會用這個視頻 "Programming Android with fragments"爲您指出正確的方向。它有點長,但它會解釋如何使用片段。

使用視頻的片段示例,並將其與來自操作欄選項卡頁面的示例代碼結合使用。

最好的運氣

**快速編輯澄清:你應該使用操作欄中的標籤,因爲這是使用標籤的最新和最簡單的方法。這是新的「最佳做法」。 TabWidget和TabActivity更老,更復雜,甚至可能會被我棄用。

+0

我使用的API級別10,所以我不認爲我能夠使用ActionBar選項卡。 –

+0

在這種情況下,我建議你認真考慮使用操作欄sherlock。 – IanB

+0

@Mohamad Ghadourian ^^是的,我忘了那件事。動作欄sherlock爲較低的api級別提供了動作欄的支持。 – scottyseus

1

我強烈建議運行和測試樣品有效的導航,它使用標籤和ViewPager

http://developer.android.com/training/implementing-navigation/index.html

+0

Salam Mohsen。感謝您的回答。我完全使用PageViewer完成了我的選項卡。我的問題是,當我想要更改片段選項卡布局時,我需要選項卡的片段對象,但是在每種方法中,我都會使用null。我會發布我的整個活動和片段。 plz向我展示我的問題到底在哪裏。非常感謝你兄弟... –

0

嘗試GrilledUI庫。它會爲你做所有骯髒的工作,甚至從XML加載標籤列表。

相關問題