我已按照本指南实施了ActionBar标签:github。但是我的选项卡指示器卡在第2和第3个选项卡之间有问题。就像有4个标签,但只显示了3个。看起来像这样:
只有当我从第3个标签滑动到第2个标签时才会这样。它只是停留在那里,如果我再次向左滑动,它会进入第二个选项卡。所以它基本上只是感觉有4个标签。
My FragmentPagerAdapter Class looks like this
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private static Context context;
private String tabTitles[];
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
Resources resources = context.getResources();
tabTitles = new String[] { resources.getString(R.string.recent),
resources.getString(R.string.popular),
resources.getString(R.string.my) };
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new RecentFragment();
case 1:
return new PopularFragment();
case 2:
return new MyFragment();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public static class RecentFragment extends Fragment implements OnItemClickListener {
ListView listView;
List<RowItem> rowItems;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.custom_list, container, false);
String[] titles = { "titleA", "titleB", "titleC" };
String[] descriptions = { "a", "b", "c" };
Integer[] images = { R.drawable.christoffer, R.drawable.frede, R.drawable.sofie };
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
listView = (ListView) rootView.findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(context,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
}
public static class PopularFragment extends Fragment {
}
public static class MyFragment extends Fragment {
}
}
我的SlidingTabLayout和SlidingTabStrip类看起来很像指南中的类。在MainActivity中,我添加了此代码来实现选项卡:
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(
getSupportFragmentManager(), MainActivity.this));
// Give the SlidingTabLayout the ViewPager
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
// Center the tabs in the layout
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
// Customize tab color
slidingTabLayout
.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return Color.RED;
}
});
答案 0 :(得分:0)
请通过java文件将片段XML加载到所有选项卡中,它将解决..