2013-10-25 130 views
1

首先,我知道有很多關於同一主題的問題,但我嘗試了很多解決方案,但我沒有達到它的效果。ListView選中突出顯示

我有一個應用程序DrawerLayout,我想改變代碼選擇的項目,選項更改但項目不突出,我不知道爲什麼。在版本中,它搞亂工作> = 2.3(API 9) 如果我手動點擊它的工作原理

項目的佈局是:我把android:focusableInTouchMode爲假,因爲我讀,如果它被啓用所選項目不工作。 我放了一個背景。

的choiceMode:drawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/llItem" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:background="@drawable/list_selector_holo_light" 
    android:focusableInTouchMode="false"> 

    <ImageView 
     android:id="@+id/iconoMenu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:adjustViewBounds="true" 
     android:paddingLeft="5dp" 
     android:paddingRight="12dp"/> 

    <TextView 
     android:id="@+id/etTitulo" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_vertical" 
     android:gravity="center_vertical" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/menu_item"/> 

</LinearLayout> 

的xml文件的背景:

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

    <item android:state_window_focused="false" android:drawable="@android: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_disabled_holo_light" /> 
    <item android:state_focused="true" android:state_enabled="false"        android:drawable="@drawable/list_selector_disabled_holo_light" /> 
    <item android:state_focused="true"        android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_light" /> 
    <item android:state_focused="false"        android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_light" /> 
    <item android:state_focused="true"                android:drawable="@drawable/list_focused_holo" /> 
    <item android:state_focused="false" android:state_selected="true" android:drawable="@drawable/list_pressed_holo_light"/> 

</selector> 

於是我emule的關於該項目的點擊與此代碼(家是所選擇的選項):

drawerList.performItemClick(drawerList, home, drawerList.getItemIdAtPosition(home)); 

drawerList.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView parent, View view, 
      int position, long id) { 
     changeOption(position, true); 

     view.setSelected(true); 

    } 
}); 


private void changeOption(int position, Boolean cerrar) { 
    fragment = new Home(); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 

    fragmentManager.beginTransaction() 
    .replace(R.id.content_frame, fragment).commit(); 

    tituloSeccion = ((MenuLateralItem) opcionesMenu[position]) 
    .getTitulo(); 
    getSupportActionBar().setTitle(tituloSeccion); 

    if ((cerrar) && (!isDrawerLocked)) { 
     drawerLayout.closeDrawer(drawerList); 
    } 
} 

謝謝!抱歉我的英語。

回答

2

對此,一個簡單的解決方法是將選定項目的值保存在適配器中(如果DrawerLayout)。然後,當你在進行convertView,檢查當前產品的選擇之一,並相應地設置其定製backgrround

public class YourDrawerAdapter extends BaseAdapter { 

    int selectedItem; 
    // all your code goes here 


    //call this method in the onItemClick listener of the listview 

    public void setSelectedItem(int position) { 
     selectedItem = position; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     //the code for the generating the view.. 

     //check here if the current item is the selected one 
     if(position == selectedItem) { 
      //set the selected layout for listItem 
     } else { 
      //set the normal layout for listItem 
     } 
     return convertView; 
    } 
} 
+0

是的,我試了一下,但應用程序只調用方法getView第一次。這是一個數組適配器。感謝您的回答 – user1852854

+0

選擇項目後,在您的適配器上調用'notifyDataSetChanged()'。它將重新填充列表視圖。 –

+0

我還沒有能夠測試。我解決了這個問題。非常感謝你! – user1852854