Android 方法“;onCreateView“;“课堂内”;占位符片段“; public View onCreateView(布局、充气机、视图组容器、, Bundle savedInstanceState){ ArrayList rootView=新的ArrayList(); 视图根1=充气机。充气(R.layout.fragment1\u主页,容器,错误); 视图根2=充气机。充气(R.layout.fragment2\u主页,容器,错误); 视图根3=充气机。充气(R.layout.fragment3\u主页,容器,错误); 视图根4=充气机。充气(R.layout.fragment4\u主页,容器,错误); 添加(root1); 添加(root2); 添加(root3); 添加(root4); 开关(getArguments().getInt(参数节号)){ 案例1: 视图v=rootView.get(0); 按钮butt1=(按钮)v.findViewById(R.id.butt1); 最终文本视图文本1=(文本视图)v.findviewbyd(R.id.section_标签); butt1.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ text1.setText(“你好”); } }); 返回v; 案例2: 返回rootView.get(1); 案例3: 返回rootView.get(2); 案例4: 返回rootView.get(3); 违约: 返回null; } }
这是我的代码,Android 方法“;onCreateView“;“课堂内”;占位符片段“; public View onCreateView(布局、充气机、视图组容器、, Bundle savedInstanceState){ ArrayList rootView=新的ArrayList(); 视图根1=充气机。充气(R.layout.fragment1\u主页,容器,错误); 视图根2=充气机。充气(R.layout.fragment2\u主页,容器,错误); 视图根3=充气机。充气(R.layout.fragment3\u主页,容器,错误); 视图根4=充气机。充气(R.layout.fragment4\u主页,容器,错误); 添加(root1); 添加(root2); 添加(root3); 添加(root4); 开关(getArguments().getInt(参数节号)){ 案例1: 视图v=rootView.get(0); 按钮butt1=(按钮)v.findViewById(R.id.butt1); 最终文本视图文本1=(文本视图)v.findviewbyd(R.id.section_标签); butt1.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ text1.setText(“你好”); } }); 返回v; 案例2: 返回rootView.get(1); 案例3: 返回rootView.get(2); 案例4: 返回rootView.get(3); 违约: 返回null; } },android,android-fragments,Android,Android Fragments,这是我的代码, 使用switch处理视图和侦听器的代码太长,我不知道如何将这些代码放到独立的类文件中。请帮忙 选择这条路 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ArrayList<View> rootView = new ArrayLi
使用switch处理视图和侦听器的代码太长,
我不知道如何将这些代码放到独立的类文件中。请帮忙 选择这条路
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<View> rootView = new ArrayList<>();
View root1 = inflater.inflate(R.layout.fragment1_main_page, container, false);
View root2 = inflater.inflate(R.layout.fragment2_main_page, container, false);
View root3 = inflater.inflate(R.layout.fragment3_main_page, container, false);
View root4 = inflater.inflate(R.layout.fragment4_main_page, container, false);
rootView.add(root1);
rootView.add(root2);
rootView.add(root3);
rootView.add(root4);
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View v = rootView.get(0);
Button butt1 = (Button) v.findViewById(R.id.butt1);
final TextView text1 = (TextView)v.findViewById(R.id.section_label);
butt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text1.setText("Hello");
}
});
return v;
case 2:
return rootView.get(1);
case 3:
return rootView.get(2);
case 4:
return rootView.get(3);
default:
return null;
}
}
创建用于设置侦听器的新方法
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View root1 = inflater.inflate(R.layout.fragment1_main_page, container, false);
setListener(root1);
return root1;
case 2:
View root2 = inflater.inflate(R.layout.fragment2_main_page, container, false);
return root2
case 3:
View root3 = inflater.inflate(R.layout.fragment3_main_page, container, false);
return root3;
default:
return null;
}
}
是的,但是是否可以将每个案例作为单个类文件或方法进行剪切?因为当我完成整个开关时,时间会非常长
private void setListener(View root1) {
// you can do declaration and set Listener here
}