這將是最後一個問題,我有標籤,我保證:)(也許) ,並嘗試和錯誤我已經FragmentPagerAdapter稱爲PagerAdapter它創建一些標籤,也使我動態地創建新標籤的能力。 的問題是,新的標籤總是會得到插入到現有的標籤右邊:從以前的問題,所以添加標籤的左邊與FragmentPagerAdapter
(圖片說明:
我試過如下:
添加的「靜態條目當創建「Temp Tab」時,我可以將該條目覆蓋到我想要的任何片段上,給我一些奇怪的IllegalSTateException ...
的事情,我想會的工作最好的居然真的想不好工作:而不是與添加片段:
private final List<Fragment> mFragments = new ArrayList<Fragment>(); public void addFragment(Fragment fragment) { mFragments.add(fragment); notifyDataSetChanged(); }
使用
mFragments.add(0, fragment);
但它給與消息的IllegalStateException :
Can't change tag of fragment ChatFragment
當我嘗試添加臨時片段。 現在,兩個問題:
有周圍的片段標籤的方式,我可以拿走所有的片段標記了片刻,重建他們的所有的東西?或
有沒有更好更優雅的方式來做到這一點?或者我應該重新設計我的用戶界面,以便該選項卡可以用於正確的? :P
編輯:這裏是一些代碼:(我再也不想只是後死亡牆的人,因爲它可能他們嚇跑:O)
FragmentPagerAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
private Context mContext;
public PagerAdapter(FragmentManager fm, Context context) {
super(fm);
mContext = context;
}
private final List<Fragment> mFragments = new ArrayList<Fragment>();
public void addFragment(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public String getPageTitle(int position) {
switch (position) {
case 0:
return mContext.getString(R.string.tab_title_chats);
case 1:
return mContext.getString(R.string.tab_title_contacts);
case 2:
return mContext.getString(R.string.tab_title_create);
}
return "WOOPS";
}
}
MainActivty :
public class LaunchActivity extends FragmentActivity implements ActionBar.TabListener {
private PagerAdapter mAdapter;
private FragmentManager man;
private ViewPager mViewPager;
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_container);
// Set up the action bar.
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Layout stuff
mViewPager = (ViewPager) findViewById(R.id.masterViewPager);
// Creating the mAdapter and initializing the mViewPager
man = getSupportFragmentManager();
mAdapter = new PagerAdapter(man, getApplicationContext());
mViewPager.setAdapter(mAdapter);
for (int i = 0; i < 2; i++) {
makeFragments(i);
System.out.println("Baking tasty fragment instances...nom nom nom");
}
// Called when tab changes
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAdapter.getCount(); i++) {
System.out.println("Working...");
actionBar.addTab(actionBar.newTab().setText(mAdapter.getPageTitle(i)).setTabListener(this));
}
}
// Add fragment instances to ArrayList depending on their future position on the ActionBar
private void makeFragments(int i) {
if (i == 0) {
mAdapter.addFragment(ChatFragment.getInstance());
}
else if (i == 1) {
mAdapter.addFragment(ContactFragment.getInstance());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.layout_container, menu);
return true;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
public void addTab(int i) {
mAdapter.addFragment(ComposeFragment.getInstance());
System.out.println("Created new Fragment, now adding it as a tab");
actionBar.addTab(actionBar.newTab().setText(mAdapter.getPageTitle(i)).setTabListener(this));
System.out.println("Added it as a tab. Hurray");
}
/** IGNORE BELOW **/
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
堆棧跟蹤:
03-18 18:32:29.274: E/AndroidRuntime(26435): FATAL EXCEPTION: main
03-18 18:32:29.274: E/AndroidRuntime(26435): java.lang.IllegalStateException: Can't change tag of fragment ChatFragment{41492428 #1 id=0x7f070004 android:switcher:2131165188:1}: was android:switcher:2131165188:1 now android:switcher:2131165188:2
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:398)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.view.ViewPager.populate(ViewPager.java:992)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:875)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2690)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
03-18 18:32:29.274: E/AndroidRuntime(26435): at com.package.subpackage.components.PagerAdapter.addFragment(PagerAdapter.java:25)
03-18 18:32:29.274: E/AndroidRuntime(26435): at com.package.subpackage.someapp.core.LaunchActivity.addTab(LaunchActivity.java:116)
03-18 18:32:29.274: E/AndroidRuntime(26435): at com.
com.package.subpackage.fragments.full.ChatFragment$1.onClick(ChatFragment.java:51)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.view.View.performClick(View.java:4204)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.view.View$PerformClick.run(View.java:17355)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.os.Handler.handleCallback(Handler.java:725)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.os.Handler.dispatchMessage(Handler.java:92)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.os.Looper.loop(Looper.java:137)
03-18 18:32:29.274: E/AndroidRuntime(26435): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-18 18:32:29.274: E/AndroidRuntime(26435): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 18:32:29.274: E/AndroidRuntime(26435): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 18:32:29.274: E/AndroidRuntime(26435): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 18:32:29.274: E/AndroidRuntime(26435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 18:32:29.274: E/AndroidRuntime(26435): at dalvik.system.NativeStart.main(Native Method)
發佈您的整個代碼如果可能的話後整個堆棧跟蹤 – Triode 2013-03-18 17:21:25
完成。添加了MainActivity(其中包含所有選項卡管理),並且addFragment函數中的PagerAdapter – AreusAstarte 2013-03-18 17:33:42
將此行代碼更改爲mFragments.add(fragment);到mFragments.add(0,fragment);這個。 – Triode 2013-03-18 17:37:58