简体   繁体   中英

Let Fragments use the Same Method

I have two fragments (each a tab in a sliding tab activity) in my application. The two fragments have some methods which now are identical, I thought that I could abstract out those methods to follow the DRY (don't repeat yourself) principle. Is there any recommended way of doing this?

Is a util class with static methods a good way? Or should I create an abstract class "MyAbstractFragment" which has those methods and let the fragments extend this class?

For example.

public class MyCustomFragment extends Fragment {
    protected LinearLayout linearLayout;
    protected MyAdapter adapter;
    //more common fields

    void addButtonToFragmentView(final String btnText) {
        final Button btn = new Button(getContext());
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                linearLayout.removeView(btn);
            }
        });
        btn.setText(btnText);
        linearLayout.addView(btn);
    }

    void upDateAdapterList(List<String> list){
        //....
        adapter.updateList(list);
    }
}

Than my fragments could extend this class and set the properties and use the methods. But I also see that I as well could make a static util class just for the methods, like addButtonToFragmentView(final String btnText, Context context, final LinearLayout linearLayout) and upDateAdapterList(List<String> list, MyAdapter adapter)

Or is there a preferred way of doing this?

Yes, you can use abstract class like below:

public abstract class BaseFragment extends Fragment {

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
  }

  public Context getContext() {
    return this.getActivity().getApplicationContext();
  }

  protected abstract void addButtonToFragmentView(final String btnText);

  protected abstract void upDateAdapterList(List<String> list){
}

then extends your new fragment with this base class.

code taken from: https://github.com/spirosoik/AndroidArchitecturePadawans/blob/master/presentation/src/main/java/com/architecture/padawans/views/common/BaseFragment.java

We should try to follow composition over inheritance. May be you can have a dedicated UIFactory class which deals with dynamic creation of views, then you move your addButtonToFragmentView method to the UI factory and make if more generic.

void addButtonToView(final String btnText, final Context, final View parentView);

As far as upDateAdapterList is concerned you can create a BaseListFragment and move it there, so whoever is interested in using a fragment with List can extend this Fragment. Hence this follows Single Responsibility Principle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM