开发者

Focus in ListField?

I have added two list fields to a screen but on a change in focus it does not go to the second list horizontally.

Focus in ListField?

Focus in ListField?

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);

        开发者_开发知识库}
}

i want on focus frim list 1 to list 2 it move horizontally .


We have encountered a similar issue when having 4 fields organized in two horizontal managers. A scroll down from the upper right field brought us to the upper left rather than the bottom right, since the movement first scrolled inside the horizontal manager.

We ended up implementing our own Manager for adding and positioning the fields (instead of the horizontal managers), then overriding the Manager's navigationMovement function, like explained in this tutorial, to control which field gets the focus depending on the movement. For example:

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

Basically dx is positive/negative depending on the move in the horizontal line and dy is positive/negative depending on the move in the vertical line.

Hope this helps, if you find a better way I'd be very happy to learn about it since this is quite cumbersome...

EDIT - CODE: I added some code sample, however we only have 4 buttons so navigation is different than what you are looking for. We Create the manager in the screen and add each field to it, and add the Manager to the screen. We have 2 methods we override in Manager, sublayout and navigationMovement.

The sublayout function is responsible for layouting your buttons, notice you have to explicitly set the x and y position, it is a good idea to use the Display.getWidth when determining the position so it keeps the same proportions on the different screens.

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);
}

For navigationMovement, use the value of dx and dy and the index of the field that currently has focus to determine the next field to receive focus. If there should be no focus change simply return false without changing the focus, for example when moving left from a leftmost field you probably want to stay on the same field and not wrap around.

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;
}

I hope this helps :)


there is another solution, where you may use any manager constructions:

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;
        }
    }

but in this case you'll need to do this for every fucusable field

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜