我有一个ViewPager/FragmentPagerAdapter组合,带有负页边距,以便页面略微重叠。问题是,中心页面不在顶部。它左边的页面遮挡了它。我希望中心页面始终显示在顶部。有没有一种优雅的方法来实现这个目标?
我有一个ViewPager/FragmentPagerAdapter组合,带有负页边距,以便页面略微重叠。问题是,中心页面不在顶部。它左边的页面遮挡了它。我希望中心页面始终显示在顶部。有没有一种优雅的方法来实现这个目标?
view.bringToFront()
更改它的z-order。public class MyActivity extends Activity {
private ViewPager vp = null;
private SparseArray<View> viewCollection = new SparseArray<View>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
vp = (ViewPager)findViewById(R.id.viewpager);
vp.setPageMargin(-20);
vp.setOffscreenPageLimit(3);
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageSelected(int pos) {
View view = viewCollection.get(pos); // @@@ Get target page reference
view.bringToFront(); // @@@ change z-order to the top
}
});
vp.setAdapter(new MyAdapter());
}
public class MyAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
View pageLayout = getLayoutInflater().inflate(R.layout.page_layout, container, false);
final Integer pageId = position;
TextView pageText = (TextView)pageLayout.findViewById(R.id.page_id);
pageText.setText(String.valueOf(position));
pageText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
vp.setCurrentItem(pageId); // @@@ select the clicked page
}
});
// paint gray shades onto the page background
pageLayout.setBackgroundColor(Color.argb(255, position*40, position*40, position*40));
viewCollection.put(position, pageLayout); // @@@ store the reference
container.addView(pageLayout);
return(pageLayout);
}
@Override
public int getCount() {
return 8; // any arbitrary number
}
@Override
public float getPageWidth(int position) {
return(0.33f);
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return (view == obj);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
viewCollection.remove(position); // @@@ remove the reference
}
}
}