2012-10-15 65 views
0

看來,gridview的onitemselected行爲是由Android控制的,我怎樣才能禁用一些項目來回調onItemSelected()?Android,如何禁用一些項目上的GridView的onItemSelected?

我的代碼:

@Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, 
      long id) { 

     if (view == null) return; 

     Utils.log(TAG, "view = " + view.toString() + ",pos = " + position + " , id =" + id); 

     //I want to disable onItemSelected after positon 3: (But I failed.) 
     if (position > 3) { 
      if (mLSV != null) { 
       onItemSelected(mGridView,mLSV, mLastPosition, mLastSelectedId); 
       return; 
      } 
     } 

     if (!mGridView.isFocused()) return; 

     if (mLSV != null) { 
      mLSV.setBackgroundColor(CMainUI_Model.BG_COLOR); 
     } 
     Utils.log(TAG, "onItemSelected, pos = " + position); 

     mLSV = view; 
     mLastPosition = position; 
     mLastSelectedId = mGridView.getSelectedItemId(); 
    } 

我使用onItemSelected()來改變項目的背景就像一個重點,因爲我的d-墊導航。而且我不想在位置3之後調用onItemSelected()並將焦點放在位置3.謝謝!

+0

你就不能忽略的一些看法回調(添加某種條件開始時)? –

+0

謝謝,我可以忽略'因爲我使用D-pad導航,我使用onItemSelected在位置3之前更改項目的背景。 – herbertD

回答

0

我覆蓋了安其()回調禁用某些方向鍵事件,如果焦點達到極限。我認爲這是最好的解決方案。不要在適配器的項目上設置onFocusChangedListener,因爲它們會由Android緩存系統:convertView丟失或格式不正確。

0

這是怎麼回事?

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, 
     long id) { 

    if (view == null) return; 

    Utils.log(TAG, "view = " + view.toString() + ",pos = " + position + " , id =" + id); 

    //I want to disable onItemSelected after positon 3: 
    if (position > 3) return; 

    if (!mGridView.isFocused()) return; 

    if (mLSV != null) { 
     mLSV.setBackgroundColor(CMainUI_Model.BG_COLOR); 
    } 
    Utils.log(TAG, "onItemSelected, pos = " + position); 

    mLSV = view; 
    mLastPosition = position; 
    mLastSelectedId = mGridView.getSelectedItemId(); 
} 

試試這個方法onItemSelected ...

+0

我想跳過位置3之後的動作,例如設置項目的背景。我正在做一個由D-pad使用的應用程序,而不是鼠標。 – herbertD

+0

我試過這個,還有一些問題:如果你通過點擊D-pad RIGHT兩次去位置3的右邊,那麼你必須按下D-pad LEFT兩次,這很無聊。不管怎樣,謝謝你。 – herbertD

+1

聽起來像你真的想重寫onKey()然後...... – Shark

1

這是相當長一段時間以前,但覆蓋這些方法在您的適配器可能會有所幫助:

@Override 
public boolean isEnabled(int position) { 
    // Check if position is enabled or not 
    return true; 
} 

@Override 
public boolean areAllItemsEnabled() { 
    return false; 
} 
相關問題