Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SamplePagerAdapter extends PagerAdapter {
- String[] editorContent;
- boolean savedState = false;
- /**
- * @return the number of pages to display
- */
- @Override
- public int getCount() {
- return currentNumPages;
- }
- /**
- * @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
- * same object as the {@link View} added to the {@link ViewPager}.
- */
- @Override
- public boolean isViewFromObject(View view, Object o) {
- return o == view;
- }
- // BEGIN_INCLUDE (pageradapter_getpagetitle)
- /**
- * Return the title of the item at {@code position}. This is important as what this method
- * returns is what is displayed in the {@link SlidingTabLayout}.
- * <p>
- * Here we construct one using the position value, but for real application the title should
- * refer to the item's contents.
- */
- @Override
- public CharSequence getPageTitle(int position) {
- //TODO get file name
- return "Item " + (position + 1);
- }
- // END_INCLUDE (pageradapter_getpagetitle)
- /**
- * Instantiate the {@link View} which should be displayed at {@code position}. Here we
- * inflate a layout from the apps resources and then change the text view to signify the position.
- */
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- LinearLayout layoutContainer = new LinearLayout(appContext);
- editor = new CodeEditor(appContext);
- editor.setTag("editor" + position);
- layoutContainer.addView(editor);
- Log.d(TAG, "tagCreate = "+editor.getTag());
- // Add the newly created View to the ViewPager
- Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]"+"numpages = "+currentNumPages);
- container.addView(layoutContainer,position);
- /*
- File sdCard = Environment.getExternalStorageDirectory();
- String dir = sdCard.getAbsolutePath();
- editor.findViewWithTag("editor"+0);
- String txt = String.valueOf((FileManagment.loadFile(dir,"tempString.txt")));
- editor.setText(txt);
- Log.d(TAG,"String = "+txt);
- editor.findViewWithTag("editor"+1);
- txt = String.valueOf((FileManagment.loadFile(dir,"tempString2.txt")));
- editor.setText(txt);
- Log.d(TAG,"String = "+txt);
- */
- // Return the View
- return layoutContainer;
- }
- @Override
- public Parcelable saveState() {
- Log.d(TAG, "save state called");
- File sdCard = Environment.getExternalStorageDirectory();
- String dir = sdCard.getAbsolutePath();
- editor = (CodeEditor) editor.findViewWithTag("editor"+0);
- Log.d(TAG, "tag = "+editor.getTag());
- try {
- FileManagment.writeFile(dir,editor.getText().toString(),"tempString.txt");
- } catch (IOException e) {
- e.printStackTrace();
- }
- editor = (CodeEditor) editor.findViewWithTag("editor"+1);
- Log.d(TAG, "tag = "+editor.getTag());
- try {
- FileManagment.writeFile(dir,editor.getText().toString(),"tempString2.txt");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return super.saveState();
- }
- @Override
- public void restoreState(Parcelable state, ClassLoader loader) {
- super.restoreState(state, loader);
- savedState = true;
- Log.d(TAG, "Restore state called");
- }
- @Override
- public void notifyDataSetChanged() {
- super.notifyDataSetChanged();
- }
- /**
- * Destroy the item from the {@link ViewPager}. In our case this is simply removing the
- * {@link View}.
- */
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
- //container.removeView((View) object);
- container.removeAllViews();
- Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
- }
- }
- void addTab(Context context){
- appContext = context;
- Log.d("adlkjsdla", "current num pages = "+currentNumPages);
- mViewPager.setAdapter(adapter);
- /*
- View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
- mViewPager, false);
- mViewPager.addView(view);
- */
- adapter.instantiateItem(mViewPager,0);
- mSlidingTabLayout.addView(mViewPager);
- currentNumPages= currentNumPages+1;
- adapter.notifyDataSetChanged();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement