Добрый день всем.
Я пытаюсь макет в ConstraintLayout
в for-loop
.
Раскладка оформляется следующим образом:
public class SingleMeal extends ConstraintLayout {
private TextView food_id;
private ImageButton spacer_minus;
private ImageButton spacer_plus;
private TextView food_quantity;
public SingleMeal(Context context) {
super(context);
init();
}
private void init(){
inflate(getContext(), R.layout.activity_single_meal, this);
this.food_id = findViewById(R.id.food_id);
this.spacer_minus = findViewById(R.id.spacer_minus);
this.spacer_plus = findViewById(R.id.spacer_plus);
this.food_quantity = findViewById(R.id.food_quantity);
}
public void setFood_id(String food_id) {
this.food_id.setText(food_id);
}
public TextView getFood_quantity() {
return food_quantity;
}
public ImageButton getSpacer_minus() {
return spacer_minus;
}
public ImageButton getSpacer_plus() {
return spacer_plus;
}
}
XML макета выглядит следующим образом:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".selectMeals.SingleMeal">
<TextView
android:id="@+id/food_id"
android:layout_width="wrap_content"
android:layout_height="@dimen/edit_text_height"
android:layout_marginStart="@dimen/medium_space"
android:gravity="center"
android:textSize="@dimen/text_size"
android:layout_alignParentStart="true"/>
<LinearLayout
android:id="@+id/layout_food_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/medium_space"
android:orientation="horizontal"
android:layout_alignParentEnd="true">
<ImageButton
android:id="@+id/spacer_minus"
android:layout_width="@dimen/plus_minus"
android:layout_height="@dimen/plus_minus"
android:background="@drawable/minus"
android:contentDescription="@string/spacer"/>
<TextView
android:id="@+id/food_quantity"
android:layout_width="@dimen/info_quantity"
android:layout_height="@dimen/edit_text_height"
android:gravity="center"
android:text="0"
android:singleLine="true"
android:textSize="@dimen/text_size" />
<ImageButton
android:id="@+id/spacer_plus"
android:layout_width="@dimen/plus_minus"
android:layout_height="@dimen/plus_minus"
android:background="@drawable/plus"
android:contentDescription="@string/spacer"/>
</LinearLayout>
</RelativeLayout>
В основном задании я создаю новый объект SingleMeal
и заполняю его методами.
ConstraintLayout previousCourse = null;
for (Course course : meal.getCourses()) {
final SingleMeal food_container = new SingleMeal(this);
food_container.setId(View.generateViewId());
food_container.setFood_id(course.getName());
final TextView food_quantity = food_container.getFood_quantity();
ImageButton minus = food_container.getSpacer_minus();
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String foodQuantity = food_quantity.getText().toString();
int foodQuantityInt = Integer.parseInt(foodQuantity) > 0 ? Integer.parseInt(foodQuantity) - 1 : Integer.parseInt(foodQuantity);
food_quantity.setText(String.valueOf(foodQuantityInt));
}
});
ImageButton plus = food_container.getSpacer_plus();
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String foodQuantity = food_quantity.getText().toString();
int foodQuantityInt = Integer.parseInt(foodQuantity) + 1;
food_quantity.setText(String.valueOf(foodQuantityInt));
}
});
lunchCountainer.addView(food_container);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(lunchCountainer);
if (previousCourse == null)
constraintSet.connect(food_container.getId(), ConstraintSet.TOP , findViewById(R.id.txt_menu_date).getId(), ConstraintSet.BOTTOM,0);
else
constraintSet.connect(food_container.getId(), ConstraintSet.TOP , previousCourse.getId(), ConstraintSet.BOTTOM,0);
constraintSet.applyTo(lunchCountainer);
previousCourse = food_container;
}
Я, вероятно, делаю что-то очень неправильное, поскольку оно проходит через цикл без каких-либо ошибок, но в конце ничего не показывает.
lunchContainer
, как сказано выше, является ConstraintLayout (внутри ScrollView
):
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/lunch_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:isScrollContainer="true"
android:orientation="vertical"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
Есть идеи?