cardview с recyclerview, чья максимальная высота должна быть в несколько dp и minheight, чтобы обернуть содержимое - PullRequest
0 голосов
/ 04 ноября 2019

У меня CardView с RecyclerView.

 <androidx.cardview.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        android:layout_margin="8dp"
                        app:cardBackgroundColor="@android:color/white"
                        app:cardCornerRadius="4dp"
                        app:cardElevation="4dp">

                        <androidx.recyclerview.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@android:color/white" />
                    </androidx.cardview.widget.CardView>

Указанная высота "300dp" должна быть максимальной высотой этой карты. Если для RecyclerView меньше элементов, тогда это должно быть wrap_content.

. В основном высота должна быть обернута с максимальной высотой 300dp.

Пожалуйста, объясните мне, как этого добиться.

Он внутри NestedScrollView.
Я пробовал с высотой 0dp для CardView.
У меня есть еще 3 такие карты с удержанием RecyclerViews.

Ответы [ 3 ]

1 голос
/ 04 ноября 2019

Попробуйте это

ViewTreeObserver vto = yourRecyclerView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        yourRecyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
        finalHeight = yourRecyclerView.getMeasuredHeight();
        finalWidth = yourRecyclerView.getMeasuredWidth();
        Log.d(TAG, "height: " + finalHeight + " width: " + finalWidth);
        if (finalHeight < 300) {
            yourCardView.setLayoutParams(new CardView.LayoutParams(
                    CardView.LayoutParams.MATCH_PARENT, finalHeight));
            yourCardView.setMinimumHeight(10);
        }
        return true;
    }
});

Надеюсь, это поможет Вам!

Спасибо.

0 голосов
/ 04 ноября 2019

Вы установили макет вашей карты android:layout_height как wrap_content и добавили еще один атрибут для максимальной высоты CardView android:maxHeight="300dp"

           <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="300dp"
                android:layout_margin="8dp"
                app:cardBackgroundColor="@android:color/white"
                app:cardCornerRadius="4dp"
                app:cardElevation="4dp">

                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white" />
            </androidx.cardview.widget.CardView>
0 голосов
/ 04 ноября 2019

Попробуйте добавить android:minHeight="300dp" и android:maxHeight="wrap_content", тогда высота макета должна быть android:layout_height="wrap_content", поэтому ваш код должен выглядеть следующим образом

           <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="300dp"
                android:maxheight="wrap_content"
                android:layout_margin="8dp"
                app:cardBackgroundColor="@android:color/white"
                app:cardCornerRadius="4dp"
                app:cardElevation="4dp">

                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white" />
            </androidx.cardview.widget.CardView>

это должно работать

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...