Blackberry how to design the Screen like grid view

拜拜、爱过 提交于 2019-12-25 02:29:41

问题


I am very new to blackberry development and i don't even know how to start. I already read some part of it from it's official site

http://developer.blackberry.com/devzone/files/design/bb7/UI_Guidelines_BlackBerry_Smartphones_7_1.pdf

and other so many link, but i can not post all the link as it is saying thay if you want to post more links then you must have 10 reputation and i dont have that so sorry for that,

Now my question is i want to design layout like this http://postimg.org/image/we3leycsd/

How can i design exactly this kind of layout. I am using eclipse for Blackberry development.

Please help i already tried many things but i am not able to achieve this.

Your any kind of help would be appreciated. Thank you in advance.


回答1:


I'd create a custom HorizontalFieldManager with n VerticalFieldManagers inside, then override the add and delete methods. Here is something I made before, that should work for you, it adds the new fields to the shortest column.

StaggeredListView.java:

public class StaggeredListView extends HorizontalFieldManager
{
    private int column_spacing = 0;

    public StaggeredListView(int columns)
    {
        super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR | NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR | USE_ALL_WIDTH);

        if (columns < 1)
        {
            throw new RuntimeException("Number of columns needs to be larger than 0.");
        }

        final int width = Display.getWidth() / columns;

        for (int i = 0; i < columns; i++)
        {
            VerticalFieldManager vfm = new VerticalFieldManager(NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR | NO_HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR)
            {
                protected void sublayout(int maxWidth, int maxHeight)
                {
                    maxWidth = Math.min(width, getPreferredWidth());
                    maxHeight = Math.min(maxHeight, getPreferredHeight());
                    super.sublayout(width, maxHeight);
                    super.setExtent(width, maxHeight);
                }
            };
            super.add(vfm);
        }
    }

    public int getColumnCount()
    {
        return getFieldCount();
    }

    /**
     * Sets the spacing between columns.
     * 
     * <p>
     * Spacing between fields is <i><b>not</b></i> set.
     * </p>
     */
    public void setColumnSpacing(int spacing)
    {
        if (spacing < 0) throw new RuntimeException("Column spacing my not be negative.");

        int length = getColumnCount();
        for (int i = 1; i < length; i++)
        {
            ((VerticalFieldManager) getField(i)).setPadding(0, 0, 0, spacing);
        }

        column_spacing = spacing;
    }

    /**
     * Get the value currently assigned via the {@link #setColumnSpacing(int)} method.
     * 
     * @return
     */
    public int getColumnSpacing()
    {
        return column_spacing;
    }

    /**
     * Deletes all fields from each of the columns.
     */
    public void clear()
    {
        int length = getColumnCount();
        for (int i = 0; i < length; i++)
        {
            ((VerticalFieldManager) getField(i)).deleteAll();
        }
    }

    /**
     * Delete specified field from the columns.
     * 
     * <p>
     * Does <b><i>not</i></b> rearrange fields.
     * </p>
     */
    public void delete(Field field)
    {
        int length = getColumnCount();
        for (int i = 0; i < length; i++)
        {
            try
            {
                ((VerticalFieldManager) getField(i)).delete(field);
                break;
            } catch (IllegalArgumentException e)
            {
                // field not in this manager
            }
        }
    }

    /**
     * Adds the field to the column with the least height.
     */
    public void add(Field field)
    {
        // find the vfm with least height
        int index = 0;
        int height = ((VerticalFieldManager) getField(index)).getPreferredHeight();

        int length = getColumnCount();
        for (int i = 1; i < length; i++)
        {
            int temp_height = ((VerticalFieldManager) getField(i)).getPreferredHeight();
            if (temp_height < height)
            {
                height = temp_height;
                index = i;
            }
        }

        ((VerticalFieldManager) getField(index)).add(field);
    }
}

As for the item's contained in it, I'd create a field with an image and text, then paint it myself (I've had a lot of issues with focus and find it easier just to use paint). You can use this to make a BaseButton http://developer.blackberry.com/bbos/java/documentation/tutorial_create_custom_button_1969896_11.html

BaseButton.java:

public abstract class BaseButton extends Field
{
    // flags to indicate the current visual state
    protected boolean _visible = true;
    protected boolean _active;
    protected boolean _focus;

    protected boolean drawfocus = false;

    private int touch_top = 0;
    private int touch_right = 0;
    private int touch_bottom = 0;
    private int touch_left = 0;

    protected boolean fire_on_click = true; // false fires on unclick

    public BaseButton()
    {
        this(0);
    }

    public BaseButton(long style)
    {
        super((style & Field.NON_FOCUSABLE) == Field.NON_FOCUSABLE ? style : style | Field.FOCUSABLE);
    }

    /**
     * Sets the radius around the button to trigger touch events.
     * <p>
     * (0,0,0,0) by default.
     * </p>
     */
    public void setTouchRadius(int top, int right, int bottom, int left)
    {
        touch_top = top;
        touch_right = right;
        touch_bottom = bottom;
        touch_left = left;
    }

    protected void onFocus(int direction)
    {
        _focus = true;
        invalidate();
        super.onFocus(direction);
    }

    protected void onUnfocus()
    {
        if (_active || _focus)
        {
            _focus = false;
            _active = false;
            invalidate();
        }
        super.onUnfocus();
    }

    public void set_visible(boolean visible)
    {
        _visible = visible;
        invalidate();
    }

    public boolean is_visible()
    {
        return _visible;
    }

    protected void drawFocus(Graphics g, boolean on)
    {
        if (drawfocus) super.drawFocus(g, on);
    }

    protected void layout(int width, int height)
    {
        setExtent(Math.min(width, getPreferredWidth()), Math.min(height, getPreferredHeight()));
    }

    protected boolean keyUp(int keycode, int time)
    {
        if (Keypad.map(Keypad.key(keycode), Keypad.status(keycode)) == Characters.ENTER)
        {
            _active = false;
            invalidate();
            return true;
        }

        return false;
    }

    protected boolean keyDown(int keycode, int time)
    {
        if (Keypad.map(Keypad.key(keycode), Keypad.status(keycode)) == Characters.ENTER)
        {
            _active = true;
            invalidate();
        }

        return super.keyDown(keycode, time);
    }

    protected boolean keyChar(char character, int status, int time)
    {
        if (character == Characters.ENTER)
        {
            clickButton();
            return true;
        }

        return super.keyChar(character, status, time);
    }

    protected boolean navigationClick(int status, int time)
    {
        if (status != 0)
        { // non-touch event
            _active = true;
            invalidate();
            if (fire_on_click) clickButton();
        }
        return true;
    }

    protected boolean trackwheelClick(int status, int time)
    {
        if (status != 0)
        { // non-touch event
            _active = true;
            invalidate();
            if (fire_on_click) clickButton();
        }
        return true;
    }

    protected boolean navigationUnclick(int status, int time)
    {
        if (status != 0)
        { // non-touch event
            _active = false;
            invalidate();
            if (!fire_on_click) clickButton();
        }
        return true;
    }

    protected boolean trackwheelUnclick(int status, int time)
    {
        if (status != 0)
        { // non-touch event
            _active = false;
            invalidate();
            if (!fire_on_click) clickButton();
        }
        return true;
    }

    protected boolean invokeAction(int action)
    {
        switch (action)
        {
            case ACTION_INVOKE :
            {
                clickButton();
                return true;
            }
        }

        return super.invokeAction(action);
    }

    protected boolean touchEvent(TouchEvent message)
    {
        boolean isOutOfBounds = touchEventOutOfBounds(message);
        switch (message.getEvent())
        {
            case TouchEvent.CLICK :
                if (!_active)
                {
                    _active = true;
                    invalidate();
                }

                if (!isOutOfBounds)
                {
                    if (fire_on_click) clickButton();
                    return true;
                }

            case TouchEvent.DOWN :
                if (!isOutOfBounds)
                {
                    if (!_active)
                    {
                        _active = true;
                        invalidate();
                    }
                    return true;
                }
                return false;

            case TouchEvent.UNCLICK :
                if (_active)
                {
                    _active = false;
                    invalidate();
                }

                if (!isOutOfBounds)
                {
                    if (!fire_on_click) clickButton();
                    return true;
                }

            case TouchEvent.UP :
                if (_active)
                {
                    _active = false;
                    invalidate();
                }

            default :
                return false;
        }
    }

    private boolean touchEventOutOfBounds(TouchEvent message)
    {
        int x = message.getX(1);
        int y = message.getY(1);
        return (x < 0 - touch_left || y < 0 - touch_top || x > getWidth() + touch_right || y > getHeight() + touch_bottom);
    }

    public void setDirty(boolean dirty)
    {
    }

    public void setMuddy(boolean muddy)
    {
    }

    public void clickButton()
    {
        if (_visible) fieldChangeNotify(0);
    }
}

ImageSubtitleButton.java:

public class ImageSubtitleButton extends BaseButton
{
    private static final int FOCUS_THINKNESS = 2;
    String title;
    Bitmap image_default;

    int height;

    public ImageSubtitleButton(String title, String image_default)
    {
        this.image_default = Bitmap.getBitmapResource(image_default);
        setTitle(title);
    }

    public void setTitle(String title)
    {
        this.title =  title;
        height = image_default.getHeight() + getFont().getHeight()  + (FOCUS_THINKNESS * 2);

        updateLayout();
        invalidate();
    }

    public int getPreferredWidth()
    {
        return Math.max(getFont().getAdvance(title), image_default.getWidth());
    }

    public int getPreferredHeight()
    {
        return height;
    }

    protected void paint(Graphics graphics)
    {
        int x = (getWidth() - image_default.getWidth()) / 2;
        int y = 0;
        graphics.drawBitmap(x, y, image_default.getWidth(), image_default.getHeight(), image_default, 0, 0);

        if (_focus)
        {
            graphics.setColor(Color.BLUE); // your focus colour
            for (int i = 0; i < FOCUS_THINKNESS; i++)
            {
                graphics.drawRect(x + i, y + i, image_default.getWidth() - (i * 2), image_default.getHeight() - (i * 2));
            }
        }

        graphics.setColor(Color.BLACK);
        y = image_default.getHeight();
            graphics.drawText(title, x, y);
    }
}

Now you can add these to your screen as follows:

StaggedListView listview = new StaggedListView(2);
ImageSubtitleButton button = new ImageSubtitleButton("test", "test.png");
listview.add(button);
add(listview);

You'll need to set the preferred width and height of the ImageSubtitleButton to keep it uniform, as in the example image you posted.




回答2:


Apologies, I don't have time to create a full answer, but I personally would not use the HFM/VFM combination to do this. Instead use one Manager that provides the Grid. If you are using a late enough level OS you have GridFieldManager that does this,

GridFieldManager

but I have had mixed experiences with that Manager. So I generally use this Manager:

TableLayoutManager

I hope this gets you going.



来源:https://stackoverflow.com/questions/24115228/blackberry-how-to-design-the-screen-like-grid-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!