基本上我有多个标签,它们都引用相同的.xml
文件,几乎都做同样的事情。我想创建一个Fragment
类,然后在其他五个Fragment
类中重用其全部内容。
所以这是我的template
类 - 我希望所有fragments
重用的类:
public class TabTemplate extends Fragment {
TimePicker timePickerOne;
EditText forename;
EditText surname;
EditText email;
EditText telephone;
Button store;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabfill, container, false);
timePickerOne = (TimePicker) rootView.findViewById(R.id.timePickers);
forename = (EditText) rootView.findViewById(R.id.forename);
surname = (EditText) rootView.findViewById(R.id.surname);
email = (EditText) rootView.findViewById(R.id.email);
telephone = (EditText) rootView.findViewById(R.id.telephone);
store = (Button) rootView.findViewById(R.id.store);
return rootView;
}
@Override
public void onStart() {
super.onStart();
}
public void populate(){
...
}
}
目前我有这样的事情:
TabOne
:
public class TabOne extends Fragment {
TimePicker timePickerOne;
EditText forename;
EditText surname;
EditText email;
EditText telephone;
Button store;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabfill, container, false);
timePickerOne = (TimePicker) rootView.findViewById(R.id.timePickers);
forename = (EditText) rootView.findViewById(R.id.forename);
surname = (EditText) rootView.findViewById(R.id.surname);
email = (EditText) rootView.findViewById(R.id.email);
telephone = (EditText) rootView.findViewById(R.id.telephone);
store = (Button) rootView.findViewById(R.id.store);
return rootView;
}
@Override
public void onStart() {
super.onStart();
}
public void populate(){
...
}
}
Class TabTwo
:
public class TabTwo extends Fragment {
TimePicker timePickerOne;
EditText forename;
EditText surname;
EditText email;
EditText telephone;
Button store;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabfill, container, false);
timePickerOne = (TimePicker) rootView.findViewById(R.id.timePickers);
forename = (EditText) rootView.findViewById(R.id.forename);
surname = (EditText) rootView.findViewById(R.id.surname);
email = (EditText) rootView.findViewById(R.id.email);
telephone = (EditText) rootView.findViewById(R.id.telephone);
store = (Button) rootView.findViewById(R.id.store);
return rootView;
}
@Override
public void onStart() {
super.onStart();
}
public void populate(){
...
}
}
正如您所看到的,TabOne
和TabTwo
几乎完全相同,这就是我尝试重用TabTemplate
类中的所有内容的原因。我该怎么做?
答案 0 :(得分:0)
只使用一个Fragment Classes并执行以下操作:
EXISTS (SELECT 1 FROM ...)
然后,您可以使用不同的public class TabTemplate extends Fragment {
TimePicker timePickerOne;
EditText forename;
EditText surname;
EditText email;
EditText telephone;
Button store;
String argOne;
// This allows you to tell the Tab Activity Bundle options for whatever you want to do
public static TabTemplate newInstance(Bundle args){
TabTemplate fragment = new TabTemplate();
if(args != null){
fragment.setArguments(args);
}
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(getArguments() != null){
// Can retrieve the arguments once it has been created
Bundle args = getArguments();
argOne = args.getString("SOME_KEY", "Default Value");
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabfill, container, false);
timePickerOne = (TimePicker) rootView.findViewById(R.id.timePickers);
forename = (EditText) rootView.findViewById(R.id.forename);
surname = (EditText) rootView.findViewById(R.id.surname);
email = (EditText) rootView.findViewById(R.id.email);
telephone = (EditText) rootView.findViewById(R.id.telephone);
store = (Button) rootView.findViewById(R.id.store);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
populate();
}
@Override
public void onStart() {
super.onStart();
}
public void populate(){
//... do something with the values here
forename.setText(argsOne);
}
}
参数
Bundle
等
祝你好运,快乐的编码!