这是我的代码:
public class FragmentLifenews extends SherlockFragment {
private ActionBar actionBar;
private ViewPager viewPager;
public FragmentLifenews() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_life, container, false);
viewPager = (ViewPager) view.findViewById(R.id.pager);
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setAdapter(new ViewPager1(getFragmentManager()));
addActionBarTabs();
return view;
}
private ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
actionBar.setSelectedNavigationItem(position);
}
};
private void addActionBarTabs() {
getSherlockActivity().getSupportActionBar();
getSherlockActivity().setSupportProgressBarVisibility(true);
getSherlockActivity().setSupportProgressBarIndeterminate(true);
String[] tabs = { "Tab 1", "Tab 2", "Tab 3" };
for (String tabTitle : tabs) {
ActionBar.Tab tab = actionBar.newTab().setText(tabTitle)
.setTabListener(tabListener);
actionBar.addTab(tab);
}
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
private ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
public class ViewPager1 extends FragmentStatePagerAdapter {
private final int PAGES = 3;
public ViewPager1(FragmentManager actionBar) {
super(actionBar);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tab1Fragment();
case 1:
return new Tab2Fragment();
case 2:
return new Tab3Fragment();
default:
throw new IllegalArgumentException("The item position should be less or equal to:" + PAGES);
}
}
@Override
public int getCount() {
return PAGES;
}
FragmentLifenews
是从SherlockFragment
延伸的类。我想用ViewPager
标签添加SherlockFragment
,但我无法收到错误:
com.locii.fragment.FragmentLifenews.onCreateView(FragmentLifenews.java:28)
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setAdapter(new ViewPager1(getFragmentManager()));
请告诉我我做错了什么我改变getFragmentManager()
代替getSupportActionBar()
请建议我。
答案 0 :(得分:0)
@SuppressLint( “ResourceAsColor”) 公共类MainActivity扩展了FragmentActivity实现 OnTabChangeListener,OnPageChangeListener {
MyPageAdapter pageAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
// Tab Initialization
initialiseTabHost();
// Fragments and ViewPager Initialization
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
mViewPager.setAdapter(pageAdapter);
mViewPager.setOnPageChangeListener(MainActivity.this);
}
// Method to add a TabHost
private static void AddTab(MainActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec) {
tabSpec.setContent(new MyTabFactory(activity));
tabHost.addTab(tabSpec);
}
// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
// mTabHost.getTabWidget().setDividerDrawable(null);
setSelectedTabColor();
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
// Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int pos = this.mViewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
// mTabHost.getTabWidget().setDividerDrawable(null);
}
@Override
public void onPageSelected(int arg0) {
}
@SuppressLint("ResourceAsColor")
private void setSelectedTabColor() {
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().setDividerDrawable(
android.R.color.transparent);
TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title);
tv.setTextColor(getResources().getColor(R.color.white));
mTabHost.getTabWidget()
.setShowDividers(TabWidget.SHOW_DIVIDER_NONE);
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(R.drawable.bottom_home_back);
// mTabHost.getTabWidget().getChildAt(0).getLayoutParams().width =
// 50;
}
mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
.setBackgroundResource(R.drawable.btn_selected);
// mTabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 50;
}
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
// TODO Put here your Fragments
// DealTab f1 = DealTab.newInstance();
DealTab_New f1 = DealTab_New.newInstance();
EventTab f2 = EventTab.newInstance();
MyAccountFragment f3 = MyAccountFragment.newInstance();
MessageFragment f4 = MessageFragment.newInstance();
MoreFragment f5 = MoreFragment.newInstance();
QrCodeFragment f6 = QrCodeFragment.newInstance();
// fList.add(f1);
fList.add(f1);
fList.add(f2);
fList.add(f3);
fList.add(f4);
fList.add(f5);
fList.add(f6);
return fList;
}
// Tabs Creation
@SuppressLint("ResourceAsColor")
private void initialiseTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
// TODO Put here your Tabs
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab1").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_deals)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab2").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_event)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab3").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_my_account)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab4").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_message)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab5").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_more)));
mTabHost.setOnTabChangedListener(this);
setSelectedTabColor();
}
} 我在我的应用程序中使用tab主机并查看pager ...