ViewPager に 3 つのページがあるアプリに取り組んでいます。しかし、ページをスワイプしているときにフラグメントを変更することはできません。誰かがそれを行う方法を理解するのを手伝ってください....
public class HomePage extends FragmentActivity {
ViewPager viewPager;
PagerTabStrip pagerTabStrip;
PagerAdapter adapter;
TextView pageTag;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
/*
* ActionBar Fragment Class
*/
ActionBar actionBar;
Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
/*
* Setting adapter for viewPager
*/
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new TabView(getSupportFragmentManager());
viewPager.setAdapter(adapter);
/*
* Setting coustom Animation for ViewPager
*/
viewPager.setPageTransformer(true, new CoustomPageTransform());
/*
* All properties for pagerTabStrip //
*/
pagerTabStrip = (PagerTabStrip) findViewById(R.id.tabstrip);
pagerTabStrip.setDrawFullUnderline(true);
pagerTabStrip.setTabIndicatorColor(Color.parseColor("#33B5E5"));
pagerTabStrip.setTextSpacing(80);
pagerTabStrip.setTextColor(Color.parseColor("#666666"));
/*
* Setting Up ActionBar Fragment
*/
if (savedInstanceState != null) {
actionBar = new ActionBar();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, actionBar)
.commit();
}
}
public class TabView extends FragmentStatePagerAdapter {
public TabView(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "TASKS";
case 1:
return "WORKBOOK";
case 2:
return "ITINERARY";
default:
break;
}
return null;
}
}
public class CoustomPageTransform implements ViewPager.PageTransformer {
float MIN_SCALE = 0.85f;
float MIN_ALPHA = 0.5f;
@Override
public void transformPage(View view, float position) {
int pageHeight = view.getHeight();
int pageWidth = view.getWidth();
if (position < -1) {
view.setAlpha(0);
} else if (position <= 1) {
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horMargin - vertMargin / 2);
} else {
view.setTranslationX(-horMargin + vertMargin / 2);
}
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
view.setAlpha(MIN_ALPHA + scaleFactor - MIN_SCALE
/ (1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else {
view.setAlpha(0);
}
}
}
}