我对 ViewPager 类有疑问。正如您在屏幕截图中看到的,正常页面从上到下按从左到右的顺序排列:右页在底部,而不是中央,最上面是左页,我想把中央(中间) 位于此堆栈顶部的页面。你怎么想,这是可能的?
现在如何:
http://oi57.tinypic.com/2hdsehd.jpg
我想要达到的目标:
http://oi60.tinypic.com/jkyddl.jpg
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mContent = inflater.inflate(R.layout.fragment_main, container, false);
mViewPager = (ViewPager) mContent.findViewById(R.id.view_pager);
mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
mViewPager.setCurrentItem(1);
mViewPager.setPageTransformer(true, new DepthPageTransformer());
return mContent;
}
class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
return new FriendsFragment();
case 1:
return new CenterFragment();
case 2:
return new MenuFragment();
}
return new FriendsFragment();
}
@Override
public float getPageWidth(int page) {
if (page != 1) {
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Log.e("", "x: " + size.x);
return 0.8f;
} else
return super.getPageWidth(page);
}
@Override
public int getCount() {
return 3;
}
}
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
Log.e("", "position: " + position + " pageWidth: " + pageWidth);
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
//view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
// view.setAlpha(1);
// view.setScaleX(1);
// view.setScaleY(1);
view.setTranslationX(pageWidth * -position );
} else if (position <= 1) { // (0,1]
// Fade the page out.
//view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position + pageWidth / 5);
// Scale the page down (between MIN_SCALE and 1)
// float scaleFactor = MIN_SCALE
// + (1 - MIN_SCALE) * (1 - Math.abs(position));
// view.setScaleX(scaleFactor);
// view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
//view.setTranslationX(pageWidth * -position + pageWidth/5);
}
}
}