3

Consider an Activity MainActivity with a fragment MainFragment. The fragment has some complex layout hierarchy and a view group Frame which comes from a library com.framer:frame_me:1.1.

If I have 2 flavours foo and bar, and I want this Frame to be there only in bar flavour and not in foo, the XML element java code and dependency. How should I do this?

I can compile the dependency using

barCompile 'com.framer:frame_me:1.1'

But what about the fragment and its XML. I don't want to write 2 variations the fragment in both flavours because I don't want to maintain the same code in 2 places.


One possible idea (probably a bad one) in my mind is that this:

  1. Move the XML element in a separate file in bar source set. Add ViewStub element in the foo source set with the same name. Now include this XML file using include in the fragment XML
  2. Add an interface to handle Frame view in main source set. Add an empty implementation in foo source set and one in bar source set. This way all logic can remain in bar while all common logic remains in main source set.

This all sounds an awfully lot of work just to write flavour specific code and xml.

4

3 回答 3

1

How about replacing the Frame tag in your XML with a FrameLayout container?

Then in the bar flavor's source code you can instantiate the Frame and say container.addView(frame). While the foo flavor will have no reference to the Frame class and will ignore the container.

This is similar to your first approach, but without having to maintain separate resource sets. And it seems reasonable, that you will have some flavor-specific java code anyway.

于 2017-03-21T12:59:45.547 回答
0

what's about build.gradle sourceSets option ? You could put Fragment and XML in bar folders and then set:

android {
    productFlavors {
         ...
    }
    sourceSets {
         bar.java.srcDirs = ['src/bar/java']
         bar.res.srcDirs = ['src/bar/res']
    }
}
于 2017-03-21T14:15:58.773 回答
0

You just need abstraction. Since resources are identified using an integer index into the R class, you can use int variables as placeholders for the layout files, and given the fact a layout element ID is searched within the active layout, you can recycle the common elements. First, create a common fragment class, with all the common elements:

public abstract class BaseFlavorFragment extends Fragment {

/*Define an interface for whatever code the fragment may need from the outside and a member for keeping reference of that. You can also use the host activity, this is just for flexibility*/
    public interface whateverThisDoes{
        void do();
    }
/*All the common fragment members go here, as protected so you can reach them from every subclass*/
    protected TextView title;
    protected Button mainButton;
    protected whateverThisDoes listener;

    public void setWhateverThisDoes(whateverThisDoes listener){
        this.listener = listener;
    }
/*Finally, create a int variable that will hold the reference to the layout file you need to use. you will set this in every flavor using the setContainer method.*/
    protected int layout = 0;

    /*this will allow you to select which XML to use
layout = R.layout.flavorlayout*/
    public abstract setContainer();

    /*Use this method to inflate any flavor members, like the Frame you mentioned*/
    public abstract void inflateComponents();
    /*Use this to set listeners, data, or anything the flavor controls do*/
    public abstract void setBehaviors();

    /*Set here anything the common controls do*/
    protected void setCommonBehaviors(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //whatever                
            }
        });
        setBehaviors();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       super.onCreateView(inflater, container, savedInstanceState);
       View view = inflater.inflate(layout, container, false);
       /*Inflate common components*/
       title =  (TextView) root.findViewById(R.id.title);
       button =  (Button) root.findViewById(R.id.button);
       /*inflate flavor components, if there's any*/
       inflateComponents(); 
       /*assign data, listeners, whatever the flavor controls do*/
       setBehaviors();          
       return view;
    }
}

Now, you can just create an implementation for Foo and Bar. if the only difference is the layout file, put everything into the base class, and set the layout file using setContainer(). If you have more differences you just need to deal with them into each abstract method. The base class can live into the the common code, the implementations, into each flavor. If you don't need to set any behavioral code from the outside, you can drop the interface.

于 2017-03-23T15:57:28.157 回答