我在我的应用程序中使用com.astuetz.PagerSlidingTabStrip
来在我的应用程序中呈现多个选项卡。我的要求是,当用户点击某些内容时,我需要添加更多标签,或者在向标签的最后右侧滑动时添加更多标签。我查看了几乎所有的stackoverflow线程,但找不到任何适当的帮助。
以下是我正在做的事情 - 布局文件(XML):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="44dp"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="true"
app:pstsIndicatorHeight="4dp"
app:pstsIndicatorColor="@color/theme_color">
</com.astuetz.PagerSlidingTabStrip>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
片段:
public class MandiEntryAddTabFragment extends BaseFragment {
private ViewPager viewPager;
private View view;
private PagerSlidingTabStrip mTabs;
private MyFragmentPagerAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment_mandi_add, container, false);
if (viewPager == null) {
viewPager = (ViewPager)view.findViewById(R.id.pager);
}
getBaseActivity().setTitle("Material Entry");
FragmentManager manager = getChildFragmentManager();
viewPager.setAdapter(mAdapter = new MyFragmentPagerAdapter(manager));
mTabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
mTabs.setViewPager(viewPager);
}
return view;
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
private ArrayList<String> tabs = new ArrayList<>();
@Override
public Fragment getItem(int item) {
Fragment fragment = null;
if (item == 0) {
fragment = new MandiEntryFragment();
} else if (item == 1) {
fragment = new MandiEntryFarmerFragment();
} else if (item == 2) {
fragment = new MandiEntryFarmerFragment();
} else if (item == 3) {
fragment = new MandiEntryFarmerFragment();
} else if (item == 4) {
fragment = new MandiEntryFarmerFragment();
}/* else if (item == 5) {
fragment = new MandiEntryFarmerFragment();
new MyFragmentPagerAdapter(getChildFragmentManager()).addTab("test " + (mAdapter.getCount() + 1));
}*/
return fragment;
}
@Override
public int getCount() {
return tabs.size();
}
@Override
public CharSequence getPageTitle(int position) {
String title = new String();
if (position == 0) {
title = "Entry";
} else if (position == 1) {
title = "Farmer 1";
} else if (position == 2) {
title = "Farmer 2";
} else if (position == 3) {
title = "Farmer 3";
} else if (position == 4) {
title = "Farmer 4";
tabs.get(position);
} /*else if (position == 5) {
title = "Add";
public CharSequence getPageTitle(int position) { return tabs.get(position);
}
}*/
return title;
}
public void addTab(String tab) {
tabs.add(tab);
notifyDataSetChanged();
mTabs.notifyDataSetChanged();
}
}
@Override
public void fetchAndShowData() {
Fragment fragment = getActiveFragment(viewPager);
if (fragment != null) {
((BaseFragment) fragment).fetchAndShowData();
}
}
}
我得到的错误是:
E / AndroidRuntime:java.lang.RuntimeException:无法启动活动 ComponentInfo {} c2c.cloud4things.com/com.cloud4things.c2c.CommunityHomeActivity: java.lang.IndexOutOfBoundsException:索引4无效,大小为0
同样在上述片段的getPageTitle
和onCreateView
函数上。
我可以理解getCount
方法存在一些问题。但我不知道该怎么做。请帮助我我们非常感谢您。在此先感谢!