0
基本上我有多個選項卡,它們都指向相同的.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
類的一切。我該怎麼做呢?
不是擴展'Fragment',擴展'TabTemplate'? – Ozgur
然後我該怎麼辦? –