1
votes

Recyclerview dans DialogFragment

J'utilise le code suivant pour créer une ressource de mise en page xml.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="@dimen/dp48"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center"
        android:fontFamily="@font/trebuchet"
        android:text="@string/select_a_folder"
        android:textColor="?attr/colorAccent"
        android:textSize="16sp"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintBottom_toTopOf="@+id/recyclerViewFolderList"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewFolderList"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="350dp"
        android:maxHeight="350dp"
        app:layout_constraintBottom_toTopOf="@+id/imgViewClose2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imgViewClose2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/close"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:src="@drawable/ic_baseline_close_24dx"
        android:text="@string/cancel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerViewFolderList"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Et lorsque je charge recyclerview avec peu d'éléments, j'obtiens le résultat attendu,

 entrez la description de l'image ici

Mais si je charge près de 50 éléments, recyclerview remplit toute la vue, donc mon titre et mon bouton d'annulation sont manquants.

 entrez la description de l'image ici

Si je règle le layout_height =" 0dp " pour recyclerview, recyclerview totalement disparu comme suit .

 entrez la description de l'image ici

Je ne veux pas corriger la taille de recyclerview; si je le répare, cela fonctionnera, mais c'est toujours fixe, même si je ne passe que 2 éléments, l'espace vide sera là.

Je veux donc que la vue de recyclage soit wrap_content et que le titre et le bouton soient visibles.


1 commentaires

Salut !, Pourquoi n'utilisez-vous pas la boîte de dialogue d'alerte pour afficher la liste à la place? Cela a l'air beaucoup plus beau. Exemple: codesfor.in /wp-content/uploads/2016/10/list-dialog-169x300.p‌ ng


7 Réponses :


0
votes

essayez d'utiliser minWidth , minHeight et maxWidth , maxHeight sur la disposition racine de la boîte de dialogue.


0 commentaires

0
votes

Dans votre ensemble Recyclerview

app:layout_constraintHeight_max="350dp"
android:maxHeight="350dp"

supprimer

android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"


0 commentaires

0
votes

utiliser cette mise en page

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/roundable_bg">

<android.support.v7.widget.RecyclerView
    android:id="@+id/wv_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bt_ok"
    android:background="@android:color/white"
    />

<TextView
    android:id="@+id/bt_ok"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="8dp"
    android:padding="12dp"
    android:layout_alignParentBottom="true"
    android:textStyle="bold"
    android:background="@drawable/button_selector"
    android:gravity="center"
    android:textColor="@android:color/black"
    android:text="@string/ok" />
    </RelativeLayout>


0 commentaires

0
votes

Je pense que la configuration de android: layout_height = "match_parent" sur ConstraintLayout résoudra votre problème.


0 commentaires

3
votes

Ce simple changement fait que cela fonctionne.

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerViewFolderList"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_default="wrap"
    app:layout_constraintBottom_toTopOf="@+id/imgViewClose2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView1" />

Merci à tous ceux qui ont tous essayé de m'aider.


4 commentaires

où est le changement!?


@AhmedAdelIsmail Regardez, j'ai changé layout_height de wrap_content à 0dp , j'ai également supprimé maxHeight et layout_constraintHeight_max < / code> attributs.


Avec cela, la boîte de dialogue ne se réduit pas lorsque le clavier s'ouvre.


app: layout_constraintHeight_default = "wrap" a corrigé le problème



1
votes

J'ai eu ce problème. Finalement, j'ai abandonné l'utilisation de ConstraintLayout ici, et je suis retourné à RelativeLayout - facile et a fonctionné comme un charme.

ConstraintLayout est génial, mais parfois la meilleure solution est la vieille école.


0 commentaires

1
votes

Après avoir ajouté app: layout_constraintHeight_default = "wrap" dans mon recyclerView, les contraintes ont commencé à fonctionner


0 commentaires