2010-10-14 27 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做到这一点字段