适配器类:嵌套在活动中?
我正在编写一个 PagerAdapter,它将与我的应用程序中的 ViewPager 一起使用。
我正在使用的示例将适配器作为 PagerViewActivity 类中的嵌套类。我相信这使它可以访问该类中的所有成员变量。
我的印象是,这只是为了简单起见,因为它是一个示例,如果我要构建一个应用程序,我应该将此 Adapter 类从我的活动类中分离出来。
如果我这样做,我是否需要从调用适配器的活动中传递上下文?另外,为了避免每次需要时都创建一个新的 LayoutInflator,我是否必须传递对此的引用?
什么是最好的或被认为是好的做法?
非常感谢, 中号
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嵌套类本身并不是一个坏习惯。使用私有嵌套类来实现特定于父活动的适配器被认为是可以接受的。如果该适配器是一个更通用的实现,旨在多次使用,那么最好将适配器分离到一个独立的类中。
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.