如何在使用ViewPager时让Fragment中的每个页面的代码

时间:2013-10-16 04:07:35

标签: android android-fragments

当我使用ViewPager时,其适配器需要加载View,这些View只能通过LayoutInflater.inflater构建。

但我不知道每个页面的代码写到哪里,我不想让所有页面的代码都在MainActivity(那控制着ViewPager),我想让每个页面的代码放在不同的文件中或者类。

你可以告诉我怎么做。:D

1 个答案:

答案 0 :(得分:0)

我想让每个页面的代码放在不同的文件或类中。

创建一个Java类,其方法为inflateViews,并将您的活动上下文传递给该方法

void inflateViews(Context context) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.view_inflated, null);


    }

LayoutInflater 的更多详情为here.