Within a RecyclerView
, I'm creating new CustomView
s and then setting their Layout Parameters, like so:
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
CustomView customView= new CustomView(parent.getContext() /*, ...otherCustomArgs*/);
customView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
// ...
}
Now I've observed that there are quite a few constructors for LinearLayout
- the class from which my CustomView
extends.
/**
{@link LinearLayout(Context)}
{@link LinearLayout(Context, AttributeSet)}
{@link LinearLayout(Context, AttributeSet, int)}
{@link LinearLayout(Context, AttributeSet, int, int)}
*/
Now obviously there's a standard way to pass the width / height at instantiation - through to one of these constructors, which is how layout_width/height
from XML layouts is passed into the class.
To keep things standard, I want to use one of these within my custom constructor, i.e:
CustomView extends LinearLayout {
CustomView(Context context, LayoutArgs layoutArgs, Object... otherCustomArgs){
this(context, ...layoutArgs);
// Use otherCustomArgs
}
}
I guess, because this custom constructor will never actually be used within XML layouts, it's not necessary to conform to standard construction, but there may be other things the default constructors are doing with XML layout_width/height
that setLayoutParams
wouldn't.