Android: comment puis-je créer un écran listview avec un contenu différent pour chaque ligne

StackOverflow https://stackoverflow.com/questions/3962722

  •  09-10-2019
  •  | 
  •  

Question

Je cherche à créer un écran de ListView similaire à l'écran de réglage du son (dans l'application intégrée dans les paramètres, voir l'image ci-dessous), à savoir que je veux des lignes d'avoir du texte + cases à cocher, d'autres lignes pour que le texte + « pop up » boutons, certaines lignes doivent avoir un texte uniquement etc.

Quelle est la meilleure façon d'y parvenir?

text alt

Était-ce utile?

La solution

Ce n'est pas un listview, c'est un PreferenceActivity. Jetez un oeil à la classe PrefereceActivity

http://developer.android.com/reference/android/preference/ PreferenceActivity.html

Si vous voulez vraiment avoir une vue différente pour chaque ligne dans un listview (ce qui est très valide), vous devrez créer votre propre classe qui étend BaseAdapter. Dans la méthode getView(), il suffit de retourner la vue que vous souhaitez afficher. Il y a beaucoup d'exemples en ligne.

Autres conseils

Comme l'a souligné Falmarri, qui n'est pas ListView mais PreferenceActivity.

Si vous ne pouvez pas travailler avec le PreferenceActivity, la façon la plus simple de faire une liste des différents éléments qui défilera si elles se développent hors de l'écran, est de placer un ScrollView autour d'un LinearLayout. Vous pouvez en lire davantage ici: http://developer.android.com/reference /android/widget/ScrollView.html

Voici un exemple très simple:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:text="Some text label here" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />
            <Button
                android:text="A button to click"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
            />
        </RelativeLayout>
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Perhaps an input field here"
        />
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:text="Some more text, and a check box" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
            />
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

Cette liste ne contient pas suffisamment d'éléments pour faire défiler, mais si vous continuez à ajouter plus d'éléments à ce qu'il va commencer à défiler dès qu'il devient plus grand que l'écran.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top