2010-10-14 63 views
1

我已將兩個列表字段添加到屏幕 ,但在焦點更改時,它不會水平轉到第二個列表。焦點在ListField中?

alt text alt text

class TestScreen extends MainScreen { 
     private final ObjectListField listField = new ObjectListField(FIELD_LEFT) 
     { 
      public void layout(int width, int height) 
       { 
       super.layout(width,height); 
       setExtent(Display.getWidth()/2, Display.getHeight()); 
       } 

     }; 
     private final ObjectListField listField2 = new ObjectListField(FIELD_RIGHT) 
     { 
      public void layout(int width, int height) 
       { 
       super.layout(width,height); 
       setExtent(Display.getWidth()/2, Display.getHeight()); 
       } 

     }; 
     private final String[] lines = { "Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6" }; 
     private final String[] lines2 = { "Line 10", "Line 20", "Line 30", "Line 40", "Line 50", "Line 60" }; 
     TestScreen() 
     { 
        super(NO_VERTICAL_SCROLL); 
        HorizontalFieldManager hfm=new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL); 
        hfm.add(listField); 
        hfm.add(listField2); 
        listField.set(lines); 
        listField2.set(lines2); 
        add(hfm); 

     } 
} 

我想重點FRIM列表1至列表2水平移動上。

+0

你能提供一個簡短的代碼片段嗎? – 2010-10-14 14:12:44

回答

2

當在兩個水平管理器中組織4個字段時,我們遇到了類似的問題。向右滾動右上角左側,而不是右側,因爲該移動首先在水平管理器內滾動。

我們最終實現我們自己的經理添加和定位領域(而非水平經理),然後重寫經理的navigationMovement功能,如在this tutorial解釋,來控制哪些領域得到取決於運動的焦點。例如:

if (dx < 0) 
{ 
    getField(focusedIndex - 1).setFocus(); 
} 

基本上dx是根據在水平行中的移動的正/負和dy是正/負根據在垂直線的移動。

希望這會有所幫助,如果你找到一個更好的辦法,我會很高興來了解它,因爲這是很麻煩......

編輯 - CODE:我添加了一些代碼示例,但我們只有4個按鈕,因此導航與您正在查找的不同。我們在屏幕上創建管理器並添加每個字段,然後將管理器添加到屏幕上。我們有2種方法,我們在管理器,子佈局和導航移動中覆蓋。

sublayout函數負責佈局按鈕,注意你必須明確地設置x和y的位置,在確定位置時使用Display.getWidth是一個好主意,所以它在不同的位置保持相同的比例屏幕。

protected void sublayout(int width, int height) 
{ 
    int count = getFieldCount(); 
    Field field; 
    for (int i = 0; i < count; i++) 
    { 
     field = getField(i); 
     layoutChild(field, field.getWidth(), field.getHeight()); 
     setPositionChild(field, xPositionOfButton, yPositionOfButton); 
    } 

    setExtent(width, height); 
} 

對於navigationMovement,使用dx和dy的值以及當前具有焦點的字段的索引來確定下一個接收焦點的字段。如果不應該改變焦點,只需在不改變焦點的情況下返回false,例如從最左邊的字段向左移動時,您可能希望保持在同一個字段而不是環繞。

public boolean navigationMovement(int dx, int dy, int status, int time) 
{ 
    int focusedIndex = getFieldWithFocusIndex(); 

    if (dx < 0) 
    { 
     getField(focusedIndex - 1).setFocus(); 
    } 
    else if (dx > 0) 
    { 
     getField(focusedIndex + 1).setFocus(); 
    } 
    else if (dy < 0) 
    { 
     getField(focusedIndex - 2).setFocus(); 
    } 
    else if (dy > 0) 
    { 
     getField(focusedIndex + 2).setFocus(); 
    } 
    else 
     return false; 

    return true; 
} 

我希望這有助於:)

+0

你可以給我你的管理員的代碼嗎?你如何管理你的焦點... – amit 2010-10-22 07:21:09

+0

@amit我在答案的主體中添加了示例代碼和評論。 – Tamar 2010-10-26 13:44:01

+0

非常感謝你的工作 – amit 2010-11-02 06:28:02

1

還有另一種解決方案,在那裏你可以使用任何管理結構:

protected boolean navigationMovement(int dx, int dy, 
      int status, int time) { 

     Field focusedIndex = ThisScreen.getFieldWithFocus().getLeafFieldWithFocus(); 
     if (focusedIndex == YourFocusableField) 
     { 
      //any needed settings here 
      if (dx < 0) 
      { 
       anyField.setFocus(); 
      } 
      else if (dx > 0) 
      { 
       anyField.setFocus(); 
      } 
      else if (dy < 0) 
      { 
       anyField.setFocus(); 
      } 
      else if (dy > 0) 
      { 
       anyField.setFocus(); 
      } 
      else 
       return false; 

      return true; 
     } 
    } 

但在這種情況下,您需要爲每個fucusable做到這一點字段