Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. class SamplePagerAdapter extends PagerAdapter {
  2.  
  3. String[] editorContent;
  4. boolean savedState = false;
  5. /**
  6. * @return the number of pages to display
  7. */
  8. @Override
  9. public int getCount() {
  10. return currentNumPages;
  11. }
  12. /**
  13. * @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
  14. * same object as the {@link View} added to the {@link ViewPager}.
  15. */
  16. @Override
  17. public boolean isViewFromObject(View view, Object o) {
  18. return o == view;
  19. }
  20.  
  21. // BEGIN_INCLUDE (pageradapter_getpagetitle)
  22. /**
  23. * Return the title of the item at {@code position}. This is important as what this method
  24. * returns is what is displayed in the {@link SlidingTabLayout}.
  25. * <p>
  26. * Here we construct one using the position value, but for real application the title should
  27. * refer to the item's contents.
  28. */
  29. @Override
  30. public CharSequence getPageTitle(int position) {
  31.  
  32. //TODO get file name
  33. return "Item " + (position + 1);
  34. }
  35. // END_INCLUDE (pageradapter_getpagetitle)
  36.  
  37. /**
  38. * Instantiate the {@link View} which should be displayed at {@code position}. Here we
  39. * inflate a layout from the apps resources and then change the text view to signify the position.
  40. */
  41. @Override
  42. public Object instantiateItem(ViewGroup container, int position) {
  43. LinearLayout layoutContainer = new LinearLayout(appContext);
  44. editor = new CodeEditor(appContext);
  45. editor.setTag("editor" + position);
  46. layoutContainer.addView(editor);
  47. Log.d(TAG, "tagCreate = "+editor.getTag());
  48.  
  49.  
  50.  
  51. // Add the newly created View to the ViewPager
  52.  
  53. Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]"+"numpages = "+currentNumPages);
  54. container.addView(layoutContainer,position);
  55.  
  56. /*
  57. File sdCard = Environment.getExternalStorageDirectory();
  58. String dir = sdCard.getAbsolutePath();
  59. editor.findViewWithTag("editor"+0);
  60. String txt = String.valueOf((FileManagment.loadFile(dir,"tempString.txt")));
  61. editor.setText(txt);
  62. Log.d(TAG,"String = "+txt);
  63.  
  64. editor.findViewWithTag("editor"+1);
  65. txt = String.valueOf((FileManagment.loadFile(dir,"tempString2.txt")));
  66. editor.setText(txt);
  67. Log.d(TAG,"String = "+txt);
  68. */
  69. // Return the View
  70.  
  71. return layoutContainer;
  72. }
  73.  
  74. @Override
  75. public Parcelable saveState() {
  76. Log.d(TAG, "save state called");
  77. File sdCard = Environment.getExternalStorageDirectory();
  78. String dir = sdCard.getAbsolutePath();
  79. editor = (CodeEditor) editor.findViewWithTag("editor"+0);
  80. Log.d(TAG, "tag = "+editor.getTag());
  81. try {
  82. FileManagment.writeFile(dir,editor.getText().toString(),"tempString.txt");
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. editor = (CodeEditor) editor.findViewWithTag("editor"+1);
  87. Log.d(TAG, "tag = "+editor.getTag());
  88. try {
  89. FileManagment.writeFile(dir,editor.getText().toString(),"tempString2.txt");
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93.  
  94. return super.saveState();
  95. }
  96.  
  97. @Override
  98. public void restoreState(Parcelable state, ClassLoader loader) {
  99. super.restoreState(state, loader);
  100. savedState = true;
  101. Log.d(TAG, "Restore state called");
  102. }
  103.  
  104. @Override
  105. public void notifyDataSetChanged() {
  106. super.notifyDataSetChanged();
  107.  
  108.  
  109. }
  110.  
  111. /**
  112. * Destroy the item from the {@link ViewPager}. In our case this is simply removing the
  113. * {@link View}.
  114. */
  115.  
  116.  
  117.  
  118. @Override
  119. public void destroyItem(ViewGroup container, int position, Object object) {
  120. //container.removeView((View) object);
  121. container.removeAllViews();
  122. Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
  123. }
  124.  
  125. }
  126.  
  127. void addTab(Context context){
  128. appContext = context;
  129. Log.d("adlkjsdla", "current num pages = "+currentNumPages);
  130. mViewPager.setAdapter(adapter);
  131. /*
  132. View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
  133. mViewPager, false);
  134. mViewPager.addView(view);
  135. */
  136. adapter.instantiateItem(mViewPager,0);
  137.  
  138.  
  139. mSlidingTabLayout.addView(mViewPager);
  140. currentNumPages= currentNumPages+1;
  141. adapter.notifyDataSetChanged();
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement