A
A
Anton Charov2018-11-18 20:27:01
Java
Anton Charov, 2018-11-18 20:27:01

How to make ConstraintSet work?

From the database directory I pull out information on dishes and create a view in a cycle

ArrayList<Food> Foods = dbHelper.getFoods(spinner.getSelectedItemPosition());
        DynViewCount = 6 * Foods.size();
        int indexView = R.id.textView2;
        Map<Integer, Integer> FoodDate = dbHelper.getFoodDate();

        for (Food food : Foods){
            TextView Name = new TextView(this);
            TextView Kkal = new TextView(this);
            TextView Ing = new TextView(this);
            TextView PFC = new TextView(this);
            ImageView Photo = new ImageView(this);
            EditText Eaten = new EditText(this);
            ConstraintLayout.LayoutParams CL = new ConstraintLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            Name.setText(food.getName());
            Name.setTag(food.getId());
            Name.setId(index++);
            CL.topToBottom = indexView;
            CL.topMargin = 20;
            CL.leftMargin = 20;
            CL.leftToLeft = R.id.activityFood;
            mainL.addView(Name, CL);


            Kkal.setText(String.valueOf(food.getKkal()));
            Kkal.setId(index++);
            CL.topToBottom = Name.getId();
            mainL.addView(Kkal, CL);


            Ing.setText(food.getIng());
            Ing.setId(index++);
            CL.topToBottom = Kkal.getId();
            mainL.addView(Ing, CL);


            PFC.setText(food.getPFC());
            PFC.setId(index++);
            CL.topToBottom = Ing.getId();
            mainL.addView(PFC, CL);


            Eaten.setId(index++);
            CL.topToBottom = PFC.getId();
            if(FoodDate.containsKey(food.getId()))
                Eaten.setText(FoodDate.get(food.getId()).toString());
            mainL.addView(Eaten, CL);
            indexView = Eaten.getId();


            Photo.setImageBitmap(food.getPhoto());
            Photo.setId(index++);
            CL.topToTop = Name.getId();
            CL.leftToRight = Eaten.getId();
            CL.topMargin = 0;
            mainL.addView(Photo, CL);

            ConstraintSet set = new ConstraintSet();
            set.clone(mainL);
            set.constrainHeight(Name.getId(), ConstraintSet.WRAP_CONTENT);
            set.constrainWidth(Name.getId(), ConstraintSet.WRAP_CONTENT);
            set.constrainHeight(Kkal.getId(), ConstraintSet.WRAP_CONTENT);
            set.constrainWidth(Kkal.getId(), ConstraintSet.WRAP_CONTENT);
            set.connect(Name.getId(), ConstraintSet.TOP, indexView, ConstraintSet.BOTTOM, 20);
            set.connect(Name.getId(), ConstraintSet.LEFT, R.id.activityFood, ConstraintSet.LEFT, 20);
            set.connect(Kkal.getId(), ConstraintSet.TOP, Name.getId(), ConstraintSet.BOTTOM, 20);
            set.connect(Kkal.getId(), ConstraintSet.LEFT, R.id.activityFood, ConstraintSet.LEFT, 20);
            set.connect(Ing.getId(), ConstraintSet.TOP, Kkal.getId(), ConstraintSet.BOTTOM, 20);
            set.connect(Ing.getId(), ConstraintSet.LEFT, R.id.activityFood, ConstraintSet.LEFT, 20);
            set.connect(PFC.getId(), ConstraintSet.TOP, Ing.getId(), ConstraintSet.BOTTOM, 20);
            set.connect(PFC.getId(), ConstraintSet.LEFT, R.id.activityFood, ConstraintSet.LEFT, 20);
            set.connect(Eaten.getId(), ConstraintSet.TOP, PFC.getId(), ConstraintSet.BOTTOM, 20);
            set.connect(Eaten.getId(), ConstraintSet.LEFT, R.id.activityFood, ConstraintSet.LEFT, 20);
            set.connect(Photo.getId(), ConstraintSet.TOP, Name.getId(), ConstraintSet.TOP, 20);
            set.connect(Photo.getId(), ConstraintSet.LEFT, Eaten.getId(), ConstraintSet.RIGHT, 20);

            set.applyTo(mainL);


        }

However, all the same, all views are clustered in the upper left corner. How to do it wisely?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-11-19
@pvpered

Oooh!
It's wise to use RecyclerView, and not to cycle like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question