当我使用ViewPager
时,其适配器需要加载View
,这些View
只能通过LayoutInflater.inflater
构建。
但我不知道每个页面的代码写到哪里,我不想让所有页面的代码都在MainActivity
(那控制着ViewPager),我想让每个页面的代码放在不同的文件中或者类。
答案 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.