constraint: constraint-layout: 1.1.0 не отображать динамически добавленное настраиваемое представление - PullRequest
0 голосов
/ 15 мая 2018

Android Studio 3.1, Gradle 4.7

Я использую реализацию 'com.android.support.constraint:constraint-layout:1.0.2'

Вот мой xml:

  <android.support.constraint.ConstraintLayout
                        android:id="@+id/categoriesContainer"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="@+id/nameContainer"
                        app:layout_constraintTop_toBottomOf="@+id/birthDateContainer">

                        <android.support.v7.widget.GridLayout
                            android:id="@+id/categoriesGridContainer"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="15dp"
                            android:layout_marginLeft="15dp"
                            android:layout_marginRight="15dp"
                            android:layout_marginStart="15dp"
                            android:layout_marginTop="@dimen/tile_preview_margin_between_tiles"
                            app:layout_constraintEnd_toEndOf="@+id/categoriesContainer"
                            app:layout_constraintStart_toStartOf="@+id/categoriesContainer"
                            app:layout_constraintTop_toBottomOf="@+id/categoriesContainer" />

                    </android.support.constraint.ConstraintLayout>

Здесь "profile_category_inactive.xml"

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/profileCategoryContainer"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="@drawable/profile_category_inactive_bg">

    <TextView
        android:id="@+id/categoryNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:ellipsize="end"
        android:singleLine="true"
        android:maxLines="1"
        android:text="Test category"
        android:textColor="#a3a3a3"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Вот фрагмент кода Java, который использует этот контейнер: Я добавляю динамически все свои собственные представления по коду Java в контейнер "categoriesGridContainer":

GridLayout categoriesGridContainer = findViewById(R.id.categoriesGridContainer);
        categoriesGridContainer.setColumnCount(columnCount);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        for (int index = 0; index < findCatagoriesList.size(); index++) {
            View currentProfileCategoryActive = null;
            if (index == 1 || index == 2 || index == 5) { // test
                currentProfileCategoryActive = inflater.inflate(R.layout.profile_category_inactive, null, false);
            } else {
                currentProfileCategoryActive = inflater.inflate(R.layout.profile_category_active, null, false);
            }
            categoriesGridContainer.addView(currentProfileCategoryActive);
            ConstraintLayout profileCategoryContainer = currentProfileCategoryActive.findViewById(R.id.profileCategoryContainer);
            GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(
                    GridLayout.UNDEFINED, GridLayout.FILL, 1f),
                    GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f));

            params.width = (int) AndroidUtil.dpToPx(this, categoryItemWidth);
            params.bottomMargin = (int) marginBetweenTilesDp;
            params.topMargin = (int) marginBetweenTilesDp;
            params.rightMargin = (int) marginBetweenTilesDp;
            params.leftMargin = (int) marginBetweenTilesDp;
            profileCategoryContainer.setLayoutParams(params);
            TextView categoryNameTextView = currentProfileCategoryActive.findViewById(R.id.categoryNameTextView);
            Category category = findCatagoriesList.get(index);
            String categoryName = LocalizedStringUtil.getLocalizedStringValue(category.getName());
            categoryNameTextView.setText(categoryName);
        }

И все работает нормально. В контейнере "categoriesGridContainer" показаны все мои просмотры.

Но если я обновлю lib до

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

и в результате контейнер "categoriesGridContainer" теперь пуст. Не отображаются все мои собственные представления, которые я добавил динамически по коду Java.

1 Ответ

0 голосов
/ 16 мая 2018

Поскольку ваши проблемы возникли после перехода на более позднюю версию ConstraintLayout, возможно, у вас возникла проблема с чем-то, что было разрешено раньше, а не сейчас или просто обрабатывается по-другому.Я хотел бы очистить первоначальный макет XML для начала.Я сделал некоторые изменения.Посмотрите, помогает это или нет.

<android.support.constraint.ConstraintLayout
    android:id="@+id/categoriesContainer"
    <!-- Don't use match_parent but 0dp and rely on the constraints to expand -->
    <!-- I assume the parent is also a ConstraintLayout. -->
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/nameContainer"
    app:layout_constraintTop_toBottomOf="@+id/birthDateContainer">

    <android.support.v7.widget.GridLayout
        android:id="@+id/categoriesGridContainer"
        <!-- No match_parent -->
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="@dimen/tile_preview_margin_between_tiles"
        <!-- Reference should be to parent and not the parent's id.
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        <!-- I question the following constraint since it will move the `GridLayout` -->
        <!-- outside of the ConstraintLayout. -->
        app:layout_constraintTop_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>
...