2012-01-19 56 views
0

我在我的XML中有一個TAbleLayout,有1行,即te標題行。其他所有行,我動態添加。我想在點擊它時點擊它&。我將clickListener添加到每一行,並能夠捕獲單擊事件。但是,當按下或按住鼠標時,我看到顏色變化,然後再次變爲正常顏色。我希望顏色保持不變,直到我單擊其他行或單擊其他位置以停用所選行。Android:選擇器在TableRow選擇中沒有按預期工作

我選擇XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- Active state --> 
<item android:state_selected="true" android:state_focused="false" 
     android:state_pressed="false" android:drawable="@android:color/transparent" />  
<!-- Inactive state--> 
<item android:state_selected="false" android:state_focused="false" 
     android:state_pressed="false" android:drawable="@android:color/transparent" /> 
<!-- Pressed state --> 
<item android:state_pressed="true" android:drawable="@color/yellow" /> 

<!-- Selected state (using d-pad) --> 
<item android:state_focused="true" android:state_selected="true" 
     android:state_pressed="false" android:drawable="@color/yellow" /> 

我的Java代碼,我動態生成行:

private void createView(TableRow tr, TextView tv, String data, int rowId) { 
    tv.setText(data); 

    tv.setTextColor(Color.WHITE); 
    tv.setPadding(20, 0, 0, 0); 

    tr.setPadding(0, 1, 0, 1); 
    //tr.setBackgroundColor(Color.TRANSPARENT); 
    tr.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.tablerowselector)); 
    tr.setId(rowId); 
    tr.setClickable(true); 
    tr.setFocusable(true); // Added thinking that if its not focusable then it can't be selected, but makes no difference 
    tr.setOnClickListener(this); 
    tr.addView(tv); 
} 

如果我在clickListener事件處理,和行的設置背景顏色基礎在設置prevRowSelectedcurrRowSelected邏輯時,可以工作。但是如何使選擇器工作,爲什麼它不工作?不能按照我想要實現它的方式工作嗎?

謝謝

任何幫助表示讚賞。

+0

你還沒有放在這裏。你在哪裏有選擇器?你在哪裏改變選擇器? – Gangnus

回答

0

朋友,選擇器,沒有按預期工作。因此,使用apprpriate邏輯和設置行的顏色來編程處理它。

感謝所有。

+1

嗨,你可以指定你做了什麼改變,使其工作? – Pallavi

0

據我所知,你必須改變選擇器。我發現食譜here

+0

我已經瀏覽了您在此提供的網站。它們都不符合我的要求。如果我必須動態地創建不同的StateListDrawables,那麼只需使用setBackground(color)聽起來更簡單和容易。 – Tvd

相關問題