IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants graphiques Android Discussion :

Layout différents dans une ListView


Sujet :

Composants graphiques Android

  1. #1
    Membre du Club
    Homme Profil pro
    Cisco
    Inscrit en
    Juillet 2012
    Messages
    71
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Cisco
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 71
    Points : 55
    Points
    55
    Par défaut Layout différents dans une ListView
    Bonjour à tous,


    Je set une ListView avec 2 types d'objets différents : Food (objet a afficher dans la liste)et SectionItem (objet qui me sert de séparateur).

    Mon rendu est comme ceci :


    J'aimerai savoir s'il est possible de créer une différence de padding ou de margin (je ne sais pas trop) entre les objets Food et SectionItem, comme sur ce screen :


    voici mon Adapter :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    public class FoodsAdapter extends ArrayAdapter<Item> {
     
        private Context context;
        private ArrayList<Item> items;
        private LayoutInflater vi;
     
        public FoodsAdapter(Context context,ArrayList<Item> items) {
            super(context,0, items);
            this.context = context;
            this.items = items;
            vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
     
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
     
            final Item i = items.get(position);
            if (i != null) {
                if(i.isSection()){
                    SectionItem si = (SectionItem)i;
                    v = vi.inflate(R.layout.activity_souscarte_list_item_titlesouscategorie, null);
     
                    v.setOnClickListener(null);
                    v.setOnLongClickListener(null);
                    v.setLongClickable(false);
     
                    final TextView sectionView = (TextView) v.findViewById(R.id.souscarte_titlesouscategorie);
                    sectionView.setText(si.getTitle());
                }else{
                    Food food = (Food)i;
                    v = vi.inflate(R.layout.activity_souscarte_list_item_elementsouscategorie, null);
     
                    final TextView title = (TextView) v.findViewById(R.id.souscarte_element_title);
                    final TextView prix =  (TextView) v.findViewById(R.id.souscarte_element_prix);
                    final TextView desc =  (TextView) v.findViewById(R.id.souscarte_element_desc);
     
                    if (title != null)
                            title.setText(food.getName());
                    if (prix != null)
                            prix.setText(Float.toString(food.getPrice())+ "€");
                    if (desc != null)
                            desc.setText(food.getDescription());
                }
            }
            return v;
        }
     
    }
    voici mon activity_souscarte_list_item_titlesouscategorie.xml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
     
        <TextView android:id="@+id/souscarte_titlesouscategorie"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/green"
            android:gravity="center"
            android:layout_margin="0dp"
            android:textColor="@color/white"
            android:textSize="19sp"/>
     
    </LinearLayout>
    et my activity_souscarte_list_item_elementsouscategorie.xml :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:background="@color/white"
        android:id="@+id/souscarte_element_layout"
        android:layout_marginLeft="17dp"
        android:layout_marginRight="17dp"
        android:padding="5dp">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:id="@+id/souscarte_element_title"
                android:textColor="@color/black"
                android:layout_weight="3"
                android:paddingLeft="5dp"/>
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:id="@+id/souscarte_element_prix"
                android:gravity="right"
                android:textColor="@color/black"
                android:layout_weight="1"
                android:paddingRight="5dp"/>
        </LinearLayout>
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:id="@+id/souscarte_element_desc"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"/>
    </LinearLayout>
    J'aimerai garder une seule ListView et je ne vois pas trop comment procéder..
    Est ce possible ?

    Merci

  2. #2
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    L'item d'une list-view fait toujours la width de celle-ci donc il faut avoir deux niveaux de layouts:

    C'est déjà le cas pour activity_souscarte_list_item_titlesouscategorie.xml
    (il suffit de rajouter un paddingLeft/paddingRight/paddingTop au choix), donc laisser le LinearLayout en noir (enfin... dans la couleur de fond du thème à priori).

    Pour le cas de l'autre, il faut une FrameLayout (background vide comme avant) qui contient un RelativeLayout (ou le layout actuel si il va bien) avec le padding (ou les margins).
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  3. #3
    Membre du Club
    Homme Profil pro
    Cisco
    Inscrit en
    Juillet 2012
    Messages
    71
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Cisco
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juillet 2012
    Messages : 71
    Points : 55
    Points
    55
    Par défaut
    Merci beaucoup Nicroman !

    Voici la modification que j'ai du apporter a activity_souscrate_list_item_elementsouscategorie.xml :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingRight="10dp"
        android:paddingLeft="10dp">
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:background="@color/white"
        android:id="@+id/souscarte_element_layout"
        android:layout_marginLeft="17dp"
        android:layout_marginRight="17dp"
        android:padding="5dp">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:id="@+id/souscarte_element_title"
                android:textColor="@color/black"
                android:layout_weight="3"
                android:paddingLeft="5dp"/>
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:id="@+id/souscarte_element_prix"
                android:gravity="right"
                android:textColor="@color/black"
                android:layout_weight="1"
                android:paddingRight="5dp"/>
        </LinearLayout>
    	<TextView android:layout_width="fill_parent"
    	    android:layout_height="wrap_content"
    	    android:textColor="@color/black"
    	    android:id="@+id/souscarte_element_desc"
    	    android:paddingLeft="5dp"
    	    android:paddingRight="5dp"/>
    </LinearLayout>
    </FrameLayout>

Discussions similaires

  1. Utiliser plusieurs layout différents dans une fenetre
    Par thierry_b dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 23/07/2009, 10h24
  2. [VB.NET] Objet .Tag dans une ListView
    Par Lois dans le forum Windows Forms
    Réponses: 7
    Dernier message: 12/10/2004, 16h35
  3. URGENT - Nombre d'enregistrements différents dans une table
    Par Jeankiki dans le forum Bases de données
    Réponses: 6
    Dernier message: 11/08/2004, 15h51
  4. [VB6] Supprimer un enregistrement dans une ListView ??
    Par Argonz dans le forum VB 6 et antérieur
    Réponses: 2
    Dernier message: 14/11/2002, 09h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo