我正在使用TabLayout和ViewPager(碎片化的webview)来获取选项卡式浏览器。我面临的问题是当我删除任何选项卡时,我的应用程序崩溃,期望选项卡布局中的第一个和最后一个选项卡。
在这种情况下,我们还会从包含Fragment的ViewPager中删除关联的WebView。
之后,我们再次添加多个标签,然后删除应用程序崩溃。
添加功能
private void addViewPagerAndTab(String tag) {
mTabTags.add(tag);
MyFragment browserWebviewFragment = new MyFragment();
Bundle bundle = new Bundle();
String timestamp = System.currentTimeMillis() + "";
bundle.putString("timestamp", timestamp);
browserWebviewFragment.setArguments(bundle);
adapter.addFrag(browserWebviewFragment, "TAB " + countTabHeading, "TAB " + countTabHeading, timestamp);
countTabHeading++;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
tabLayout.setupWithViewPager(viewPager);
tabLayout.setScrollPosition(((mTabTags.size()) + 1), 0f, true);
viewPager.setCurrentItem(((mTabTags.size()) + 1));
setupTabIcons();
}
删除功能
private void removeTabs() {
int position = tabLayout.getSelectedTabPosition();
if (deleteTabPosition > position) {
mTabTags.remove(deleteTabPosition);
tabLayout.removeTabAt(deleteTabPosition);
adapter.removeViewPagerView(deleteTabPosition);
} else if (deleteTabPosition < position) {
mTabTags.remove(deleteTabPosition);
tabLayout.removeTabAt(deleteTabPosition);
adapter.removeViewPagerView(deleteTabPosition);
tabLayout.setScrollPosition(position - 1, 0f, true);
viewPager.setCurrentItem(position - 1);
} else {
int tabCount = tabLayout.getTabCount();
if (tabCount > position + 1) {
mTabTags.remove(deleteTabPosition);
tabLayout.removeTabAt(deleteTabPosition);
adapter.removeViewPagerView(deleteTabPosition);
tabLayout.setScrollPosition(position, 0f, true);
viewPager.setCurrentItem(position);
} else if (tabCount <= position + 1) {
mTabTags.remove(deleteTabPosition);
tabLayout.removeTabAt(deleteTabPosition);
adapter.removeViewPagerView(deleteTabPosition);
tabLayout.setScrollPosition(position - 1, 0f, true);
viewPager.setCurrentItem(position - 1);
}
}
}
ViewPager适配器
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<TabInfo> mFragmentTitleList = new ArrayList<TabInfo>();
private int deleteTabPosition = -1;
class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title, String url, String timestamp) {
mFragmentList.add(fragment);
TabInfo infoTab = new TabInfo();
infoTab.setStrTabName(title);
infoTab.setStrTabUrl(url);
infoTab.setTimestamp(timestamp);
mFragmentTitleList.add(infoTab);
}
@Override
public int getItemPosition(Object object) {
Fragment fragmentTemp = (Fragment) object;
if (mFragmentList.contains(fragmentTemp)) {
return mFragmentList.indexOf((Fragment) object);
} else {
return POSITION_NONE;
}
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position).getStrTabName();
}
public View getTabView(final int position) {
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_bg, null);
TextView tv = (TextView) v.findViewById(R.id.tvTab);
tv.setText(mFragmentTitleList.get(position).getStrTabName());
final ImageView img = (ImageView) v.findViewById(R.id.ic_tab_close);
img.setImageResource(R.drawable.ic_tab_close);
img.setTag(mFragmentTitleList.get(position).getTimestamp());
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strTag = (String) v.getTag();
deleteTabPosition = -1;
if (mFragmentTitleList != null) {
for (int i = 0; i < mFragmentTitleList.size(); ++i) {
if (mFragmentTitleList.get(i).getTimestamp().equals(strTag)) {
deleteTabPosition = i;
break;
}
}
}
if (tabLayout.getTabCount() != 1) {
removeTabs();
} else {
}
}
});
return v;
}
/*
* Remove the View pager page associated with Tab
* */
public void removeViewPagerView(int position) {
mFragmentList.remove(position);
mFragmentTitleList.remove(position);
this.notifyDataSetChanged();
}
}
以下链接上的完整源代码 http://45.33.27.221/CustomTabNoida.zip
java.lang.IllegalStateException: Fragment already active
at android.support.v4.app.Fragment.setInitialSavedState(Fragment.java:574)
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:110)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1054)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:946)
at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2910)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
at com.sourcefuse.sns.views.Activities.HomeScreenActivity.addViewPagerAndTab(HomeScreenActivity.java:603)
at com.sourcefuse.sns.views.Activities.HomeScreenActivity.onClick(HomeScreenActivity.java:525)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)