在多个片段中重用一个完整片段

时间:2016-03-10 20:37:38

标签: java android android-fragments code-reuse

基本上我有多个标签,它们都引用相同的.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(){
        ...
    }

}

正如您所看到的,TabOneTabTwo几乎完全相同,这就是我尝试重用TabTemplate类中的所有内容的原因。我该怎么做?

1 个答案:

答案 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

祝你好运,快乐的编码!