2017-02-28 43 views
0

我有一個具有listview的應用程序,並且該行在點擊時突出顯示。唯一的問題是它保持突出顯示。Android上的ListView - 選定的行突出顯示但不會關閉

我的代碼是:

protected void onListItemClick(ListView l, View v, int position, long id){ 

    String item = dogToastDescriptions[position]; 

    v.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_green_light)); 


     Toast.makeText(this, item, Toast.LENGTH_LONG).show(); 
    getListView().setSelected(true); 


     } 

我意識到我需要使用類似:

getListView().setSelected(false); 

,但我想不出相當把它放在哪裏。有任何想法嗎?

感謝

回答

0

我想白了就是你的列表項的默認顏色,你應該寫列表的功能刷新顏色:

public void refreshcolor(ListView listView) 
{ 
for (int i = 0; i < listView.getCount(); i++) 
    { 
     View v = (View) listView.getChildAt(i); 
     v.setBackgroundColor(Color.WHITE); 
    } 
} 

,並調用它在你的onListItemClick

protected void onListItemClick(ListView l, View v, int position, long id){ 

refreshcolor(yourListView); 
String item = dogToastDescriptions[position]; 

v.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_green_light)); 


    Toast.makeText(this, item, Toast.LENGTH_LONG).show(); 
getListView().setSelected(true); 


    } 
+0

謝謝對於建議,但它沒有奏效。仍然保持一切突出顯示一旦點擊 –

+0

可能你應該刪除getListView()。setSelected(false);希望這有幫助 –

+0

我沒有在那裏。我把這個問題放在我的問題中,作爲我可能應該嘗試的一些建議 –

相關問題