Question

I'm writing a PagerAdapter which is to be used with a ViewPager in my application.

The example I'm working from has the adapter as a nested class in the PagerViewActivity class. I believe this gives it access to all of the member variables within that class.

I get the impression that this is solely for simplicity, as it is an example, and if I'm going to build an app I should split this Adapter class out from my activity class.

If I do this will I need to pass through the Context from the activity which calls the adapter? Also to avoid creating a new LayoutInflator every time I need it will I have to pass a reference to that through?

What's best or considered good practice?

Much appreciated, M

Was it helpful?

Solution

Nested classes aren't bad practice, per se. Using a private nested class to implement an Adapter that is specific to the parent activity is considered acceptable. If that Adapter was a more generic implementation intended to be used more than once then it is good practice to separate the Adapter out into a standalone class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top