1

I'm designing a similar interface to MathStep pictured below but my main activity already extends

public class MainActivity extends Activity implements TextWatcher

and Java doesn't have multiple inheritance.

MathStep is pretty cool

In this program, you can ViewPager between Basic, functions and extra tabs. My program is not using tabs but I am swiping between fragments of buttons in a RelativeView.

How do I have part of my screen as a ViewPager, if I'm already extending Activity? Do I need to have a separate java file? How do I link the fragments that will contain the button sets back to the original activity?

I have been working on this with a friend for, I'm not kidding, 8 hours before we resorted to asking a question here. I have looked at this and this post and they were very helpful in understanding how ViewPager and Fragments work, but in all the examples they reference full paged ViewPagers, every time we attempted to create this scenario, something either wouldn't compile, or we weren't linking our XML correctly and code wasn't running... I've searched extensively for this answer and I hope I'm not the only one who has struggled with this so others can learn.

I'll admit, part of the confusion has been the learning curve but that's why I'm doing this, to learn.

Community
  • 1
  • 1
Steven
  • 189
  • 13

1 Answers1

1

How do I have part of my screen as a ViewPager, if I'm already extending Activity? Do I need to have a separate java file? How do I link the fragments that will contain the button sets back to the original activity?

All Fragments have a reference to the Activity via the getActivity() method, but you should only use it if you really need a handle to the context.

Inheritance is not required in any way whatsoever (technicality: other than for your activity, fragment, and FragmentPagerAdapter which must inherit from their respective parent classes...). The ViewPager itself can be included in the view heirarchy by referencing it from XML. The different fragments are displayed in the ViewPager by a FragmentPagerAdapter that you will have to implement yourself, this should be a separate class. If you want, it can be an inner static class, but, do not use an inner class. Keeping the scope organized by forcing dependencies to be passed through constructors will keep your code clean.

You should start by reading the ViewPager/Fragment related documentation on d.android.com. There is example code for these things and once you understand them individually everything will come together.

Thomas Dignan
  • 6,842
  • 3
  • 35
  • 48
  • Thanks for taking the time to reply Tom. A quick update. The issue was we were tackling learning fragments and the ViewPager at the same time. We sat back, learned fragments and everything is now coming along nicely. There was just too much terminology being thrown at us at one time to effectively accomplish our goal that day. – Steven Jul 23 '13 at 10:54