适配器类:嵌套在活动? [英] Adapter class: nested in activity?

查看:169
本文介绍了适配器类:嵌套在活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个PagerAdapter这是与一个ViewPager在我的应用程序中使用。

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

我是从工作的例子有适配器作为PagerViewActivity类嵌套类。我相信这给它访问所有的类中的成员变量。

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.

我得到的IM pression,这完全是为了简单起见,因为它是一个例子,如果我要建立一个应用程序,我应该出来从我的活动类拆分此适配器类。

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.

如果我这样做,我将需要通过从它调用适配器活动的背景下?同时,为了避免每次我需要将我必须通过传递给一个参考时间创建一个新的LayoutInflator?

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?

大部分AP preciated,
中号

Much appreciated, M

推荐答案

嵌套类不是不好的做法,本身。使用私有嵌套类实现一个适配器连接到父活动被认为是可以接受的是具体的。如果该适配器是一个旨在更通用的实现被使用超过一次那么它是很好的做法,适配器分离出来成为一个独立的类。

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.

这篇关于适配器类:嵌套在活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆