Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ListView et sélection/clic

Featured Replies

Posté(e)

Bonjour à tous,

Je continue mon apprentissage de l'API android, voici ce que j'aimerais faire : lorsqu'un item d'une ListView est cliqué/sélectionné, je voudrais changer sa couleur de fond (pour indiquer quel item de la liste est sélectionné). L'item garderait cette nouvelle couleur jusqu'à ce qu'un autre item soit cliqué/sélectionné...

J'ai essayé plusieurs façons de faire, notamment en changeant le fond dans un AdapterView.OnItemClickListener, le problème est que lorsque l'adapteur recycle les vues (cf. le paramètre convertView de Adapter.getView), la vue avec le fond modifié réapparaît autre part dans la ListView.

J'ai également essayé d'utiliser l'attribut android:listSelector, mais l'item ne change de couleur que pendant la durée du clic...

Quelle est la solution à ce problème ?

Merci !

Posté(e)

Bonjour,

Je pense que la résolution de ton problème passe plus par la modification des layouts/styles.

Premièrement, pour obtenir une liste avec simple sélection, la méthode onCreate(Bundle) de ta ListActivité doit ressembler à cela :

     setListAdapter(<MonAdapter avec android.R.layout.simple_list_item_single_choice>));

       final ListView listView = getListView();

       listView.setItemsCanFocus(false);
       listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Ensuite si la puce de sélection ne te convient pas, tu peux créer ton propre layout à la place de layout.simple_list_item_single_choice. Mais attention il devra obligatoirement étendre de la classe CheckedTextView(pour que l'item soit "checkable").

Pour changer le fond d'écran en fonction de la sélection, je pense qu'il suffit juste de mettre un nouveau drawable en fond de ton nouveau layout (item) qui prend en charge le "checkage". Le fond d'écran par défaut d'un item d'une liste est :

<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_window_focused="false"
       android:drawable="@color/transparent" />

   <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
   <item android:state_focused="true" android:state_enabled="false"
       android:state_pressed="true"
       android:drawable="@drawable/list_selector_background_disabled" />
   <item android:state_focused="true" android:state_enabled="false"
       android:drawable="@drawable/list_selector_background_disabled" />

   <item android:state_focused="true" android:state_pressed="true"
       android:drawable="@drawable/list_selector_background_transition" />
   <item android:state_focused="false" android:state_pressed="true"
       android:drawable="@drawable/list_selector_background_transition" />

   <item android:state_focused="true"
       android:drawable="@drawable/list_selector_background_focus" />

</selector>

Il faut donc ajouter quelque chose comme cela :

 <item android:state_checked="true" android:state_window_focused="false"
         android:drawable="@drawable/mon_drawable_on" />
   <item android:state_checked="false" android:state_window_focused="false"
         android:drawable="@drawable/mon_drawable_off" />

   <item android:state_checked="true" android:state_pressed="true"
         android:drawable="@drawable/mon_drawable_on_pressed" />
   <item android:state_checked="false" android:state_pressed="true"
         android:drawable="@drawable/mon_drawable_off_pressed" />

   <item android:state_checked="true" android:state_focused="true"
         android:drawable="@drawable/mon_drawable_on_selected" />
   <item android:state_checked="false" android:state_focused="true"
         android:drawable="@drawable/mon_drawable_off_selected" />

   <item android:state_checked="false" android:drawable="@drawable/mon_drawable_off" />
   <item android:state_checked="true" android:drawable="@drawable/mon_drawable_on" />

Bon courage,

ichpa

Posté(e)
  • Auteur

Je vais explorer cette piste, merci beaucoup !

Posté(e)
  • Auteur

En fait je ne comprends pas trop comment un layout peut herite de CheckedTextView...Au lieu de ca, j'ai fait herite mon layout de LinearLayout et ai implemente l'interface Checkable :

public class CheckableLayout extends LinearLayout implements Checkable {

private boolean mIsChecked;

public CheckableLayout(Context context) {
	super(context);
}

public CheckableLayout(Context context, AttributeSet attrs) {
	super(context, attrs);
}

@Override
public boolean isChecked() {
	return mIsChecked;
}

@Override
public void setChecked(boolean checked) {
	mIsChecked = checked;
	//if (checked)
	//	this.setBackgroundColor(Color.GRAY);
	//else
	//	this.setBackgroundColor(Color.TRANSPARENT);
}

@Override
public void toggle() {
	mIsChecked = !mIsChecked;
}

}

Ainsi le layout pour les items de la liste ressemble a :

<?xml version="1.0" encoding="utf-8"?>
<fr.jr.test2.CheckableLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@drawable/checkable_selector">
   <TextView android:id="@+id/text"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
</fr.jr.test2.CheckableLayout>

Et checkable_selector (le fond du layout) :

<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_checked="true" android:state_window_focused="false"
         android:drawable="@color/red" />
   <item android:state_checked="false" android:state_window_focused="false"
         android:drawable="@color/black" />

   <item android:state_checked="true" android:state_pressed="true"
         android:drawable="@color/red" />
   <item android:state_checked="false" android:state_pressed="true"
         android:drawable="@color/black" />

   <item android:state_checked="true" android:state_focused="true"
         android:drawable="@color/red" />
   <item android:state_checked="false" android:state_focused="true"
         android:drawable="@color/black" />

   <item android:state_checked="false" android:drawable="@color/black" />
   <item android:state_checked="true" android:drawable="@color/red" />

</selector>

Malheureusement cela ne fonctionne pas, le selecteur ne semble jamais se trouver dans l'etat "checked".

J'ai trouve une solution alternative en changeant le background du layout directement dans le code (cf. commentaires de la methode setChecked), mais pouvoir utiliser un selecteur serait encore mieux. SI quelqu'un a une idee...

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.