我的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);
}
}
}
答案 0 :(得分:1)
只需像这样更改你的代码
switch (position) {
case 0:
return new CenterFragment();
case 1:
return new FriendsFragment();
case 2:
return new MenuFragment();
}