Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. public class MyActivity extends Activity {
  2.  
  3. private ViewPager vp = null;
  4. private SparseArray<View> viewCollection = new SparseArray<View>();
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main_activity);
  10. vp = (ViewPager)findViewById(R.id.viewpager);
  11. vp.setPageMargin(-20);
  12. vp.setOffscreenPageLimit(3);
  13. vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
  14. @Override
  15. public void onPageSelected(int pos) {
  16. View view = viewCollection.get(pos); // @@@ Get target page reference
  17. view.bringToFront(); // @@@ change z-order to the top
  18. }
  19. });
  20. vp.setAdapter(new MyAdapter());
  21. }
  22.  
  23. public class MyAdapter extends PagerAdapter {
  24.  
  25. @Override
  26. public Object instantiateItem(ViewGroup container, int position) {
  27. View pageLayout = getLayoutInflater().inflate(R.layout.page_layout, container, false);
  28. final Integer pageId = position;
  29. TextView pageText = (TextView)pageLayout.findViewById(R.id.page_id);
  30. pageText.setText(String.valueOf(position));
  31. pageText.setOnClickListener(new OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. vp.setCurrentItem(pageId); // @@@ select the clicked page
  35. }
  36. });
  37. // paint gray shades onto the page background
  38. pageLayout.setBackgroundColor(Color.argb(255, position*40, position*40, position*40));
  39.  
  40. viewCollection.put(position, pageLayout); // @@@ store the reference
  41. container.addView(pageLayout);
  42. return(pageLayout);
  43. }
  44.  
  45. @Override
  46. public int getCount() {
  47. return 8; // any arbitrary number
  48. }
  49.  
  50. @Override
  51. public float getPageWidth(int position) {
  52. return(0.33f);
  53. }
  54.  
  55. @Override
  56. public boolean isViewFromObject(View view, Object obj) {
  57. return (view == obj);
  58. }
  59.  
  60. @Override
  61. public void destroyItem(ViewGroup container, int position, Object object) {
  62. container.removeView((View)object);
  63. viewCollection.remove(position); // @@@ remove the reference
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement