2010-12-10 34 views
11

J'ai le problème suivant.Comment surligner les éléments ListView

J'ai un ListView avec des lignes personnalisées consistant en une vue d'image et une vue de texte. code xml du textview est

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="26px" 
    android:layout_marginLeft="3px" 
    android:singleLine="true" 
    android:ellipsize="end" 
    android:textColorHighlight="#FEC403" 
/> 

puis-je avoir un itemclicklistener qui fonctionne très bien et je tiens à souligner le textview qui a été cliqué en procédant comme suit.

public void onItemClick(AdapterView<?> adaptview, View clickedview, int position, 
       long id) { 
      //TODO: ACTIONS 
      String pathtofile = (String) adaptview.getItemAtPosition(position); 
      View rowview = (View) adaptview.getChildAt(position); 
      rowview.setSelected(true);} 

Je voulais que la couleur de surlignage être « # FEC403 » dans le fichier XML (qui est un orange clair), mais le highlightcolor est encore gris. Alors, comment définir la couleur de surbrillance correctement?

merci à l'avance

EDIT:

voici comment je l'ai fait enfin:

ceci est mon ListView fichier xml article:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/rowselector" 
> 

<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/musicicon" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:src="@drawable/musicicon" 
    android:paddingLeft="3px" 
/> 


<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="26px" 
    android:layout_marginLeft="3px" 
    android:singleLine="true" 
    android:ellipsize="end" 
    android:focusable="false" 
/> 

et la rowselector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_selected="true" 
    android:drawable="@color/orange" /> 
</selector> 

et enfin mon OnItemClick est très court maintenant:

@Override 
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position, 
       long id) { 
      //TODO: ACTIONS 
      clickedview.setSelected(true); 
} 
+0

duplication possible de [Android ListView Selector Color] (http: // stackoverflow.com/questions/2038040/android-listview-selector-color) – MKJParekh

+0

Malheureusement, cette technique ne peut pas être utilisée pour mettre en évidence plusieurs éléments dans la liste, car un seul élément peut être sélectionné à la fois. – faizal

Répondre

8

Vous devez utiliser un Selector.

This question et sa réponse pourrait aider ....

10
@Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) 
     { 
      for(int a = 0; a < parent.getChildCount(); a++) 
      { 
       parent.getChildAt(a).setBackgroundColor(Color.BLACK); 
      } 

      view.setBackgroundColor(Color.RED); 

     } 
0

Dans votre activité:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long itemid) { 
      itemOnclickList(position, view); 
     } 
    }); 

    public void itemOnclickList(int position, View view) {  
    if (listview.isItemChecked(position)) {       
     view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_checked));    
    } else {  
    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_uncheck));   
    } 
    adapter.notifyDataSetChanged(); 

} 

votre adaptateur:

public View getView(int position, View view, ViewGroup parent) { 
    View convertView = inflater.inflate(R.layout.listdocument_item, null);  

     ListView lvDocument = (ListView) parent; 
     if (lvDocument.isItemChecked(position)) { 
      convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_checked));    
     } else { 
      convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_uncheck));    
     } 

    return convertView; 
} 

Bonne chance!

0

Voici comment il devrait ressembler sélecteur à l'aide:

Créer un fichier dans drawable appelé selector_listview_item.xml, qui ressemble à (changer les couleurs à votre propre):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- pressed --> 
    <item android:drawable="@color/md_grey_200" android:state_pressed="true" /> 
    <!-- default --> 
    <item android:drawable="@color/white" /> 
</selector> 

Maintenant, dans votre mise en page qui décrit la ligne de la liste (par exemple layout_list_row.xml), sur la mise en page de racine ajoutez le android:background="@drawable/selector_listview_item", donc la mise en page ressemblerait à quelque chose comme:

<?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="50dp" 
    android:background="@drawable/selector_listview_item" <!-- this is the important line --> 
    android:orientation="vertical"> 

    <!-- more stuff here --> 
</LinearLayout> 

(Remarque: il se peut que vous souhaitiez ajouter android:focusable="false" sur les éléments de cette ligne listview, pour que la ligne soit cliquable, comme indiqué here)

Terminé.