Reputation: 25267
I am using this flexbox layout. I am stuck getting a behavior. I want items to be displayed to next line if dynamically added items cannot fit in FlexboxLayout
.
Flexbox Version: 0.2.2
Below is my XML code:
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/fbl_choose_category_upload"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/margin_16"
android:paddingLeft="@dimen/padding_16"
android:paddingRight="@dimen/padding_16"
app:alignContent="space_between"
app:alignItems="flex_start"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="space_between">
</com.google.android.flexbox.FlexboxLayout>
My Java code:
private void generateCategories(UserCategoryListResponse userCategoryListResponse) {
for (UserCategoryListResponse.Datum data : userCategoryListResponse.getData()) {
mFlexboxLayout.addView(getCategoryRadioButton(data));
}
}
private RadioButton getCategoryRadioButton(UserCategoryListResponse.Datum data) {
RadioButton radioButton = new RadioButton(this);
radioButton.setId(Integer.parseInt(data.getCategoryId()));
radioButton.setBackgroundResource(R.drawable.selector_choose_category);
radioButton.setButtonDrawable(null);
radioButton.setPadding(DisplayUtils.pxToDp(this, 16), DisplayUtils.pxToDp(this, 16), DisplayUtils.pxToDp(this, 16), DisplayUtils.pxToDp(this, 16));
radioButton.setText(data.getCategoryName());
radioButton.setTypeface(radioButton.getTypeface(), Typeface.BOLD);
radioButton.setEnabled(data.getIsFree().equals("1"));
radioButton.setOnCheckedChangeListener(this);
return radioButton;
}
But actually what happening is, flexbox shrinks the content. Expected outcome was that, lets say, if just three categories only could be fitted in first, then, next items should appear in next line.
If anyone can help me with this.
Upvotes: 3
Views: 2705
Reputation: 15799
I haven't tried the flexbox layout, But I have solved the same issue in the past and my approach was like this.
At the time of the new dynamic view adding, I was checking the size of its parent viewgroup with screen size and if the viewgroup size gets increases then I was adding the new row with new viewgroup and adds that view to that newly created viewgroup or else was just adding the view in the same row.
I hope this will help.
Upvotes: 1