如何创建自定义滚动总是从底层做起 [英] How to create custom scrolling always start from bottom

查看:345
本文介绍了如何创建自定义滚动总是从底层做起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我创建一个屏幕聊天包含顶部栏,定制垂直滚动管理器, EDITFIELD 来发送消息和一个发送按钮。

Here I am creating a screen for chatting that contains a top bar, custom vertical scroll manager, an Editfield to send messages and a send button.

现在我创建一个消息布局:它有 VerticalFieldManager ,它包含 EditField中是可聚焦和只读,并且两个的LabelField S代表的名字和日期。此垂直场经理取决于聊天消息的数量动态地创建。这是所有自定义滚动布局。

Now I create a message layout: it has VerticalFieldManager and it contains EditField that is focusable and readonly, and two LabelFields for name and date. This vertical field manager is created dynamically depending on the number of chat messages. This is all laid out in custom scroll.

我需要滚动,从屏幕底部开始。现在,它从屏幕上方的作品。

I need scrolling to start from the bottom of the screen. Right now, it works from top of the screen.

在屏幕底部我有一个 BasicEditField 发送按钮。

At the bottom on the screen I have a BasicEditField and send button.

每个7秒,我刷新页面,刷新页面的时候,我重新创建整个布局。所以,我需要保持专注于 BasicEditField 每当我的页面刷新。我也希望滚动功能,逆转。

Each 7 seconds, I refresh the page and when refreshing the page, I recreate the whole layout. So I need to retain focus on the BasicEditField whenever my page refreshes. And I also want the scrolling to work reversed.

下面我的垂直滚动管理器类

Here my vertical scroll manager class

public class VerticalScrollManager extends VerticalFieldManager implements
    ScrollChangeListener {


public VerticalScrollManager() {
    this(VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR|DOWNWARD, Integer.MAX_VALUE, Integer.MAX_VALUE);
}

public VerticalScrollManager(int w, int h) {
    this(VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR|DOWNWARD, w, h);
}

public VerticalScrollManager(long style) {
    this(style, Integer.MAX_VALUE, Integer.MAX_VALUE);
}

public VerticalScrollManager(long style, int w, int h) {
    super(style);
    maxVisibleHeight = h;
    maxVisibleWidth = w;

    setScrollListener(this);
}




protected void sublayout(int w, int h) {

    isScrolling = ((getStyle() & VERTICAL_SCROLL) == VERTICAL_SCROLL);

    int scrollbarWidth = isScrolling ? SCROLLBAR_WIDTH + SCROLLBAR_LEFT_MARGIN + SCROLLBAR_RIGHT_MARGIN : 0;

    visibleHeight = Math.min(h, maxVisibleHeight);
    visibleWidth = Math.min(w, maxVisibleWidth);

    int myWidth = visibleWidth - scrollbarWidth;
    super.sublayout(myWidth, visibleHeight);


    visibleHeight = getHeight();
    totalHeight = getVirtualHeight();
    visibleWidth = getWidth() + scrollbarWidth;

    setExtent(visibleWidth, visibleHeight);

    setVirtualExtent(visibleWidth, totalHeight);

    isScrolling = (visibleHeight < totalHeight);


    if (isScrolling) {
        sliderHeight = visibleHeight * visibleHeight / totalHeight;
        sliderHeight = Math.max(sliderHeight, 1);    // show at least one pixel!

        sliderXPosition = visibleWidth - SCROLLBAR_WIDTH - SCROLLBAR_RIGHT_MARGIN;

    }
}




public void scrollChanged(Manager mgr, int newX, int newY) {
if (mgr == this) {
       invalidate(newX + sliderXPosition, newY, SCROLLBAR_WIDTH + SCROLLBAR_RIGHT_MARGIN, getVisibleHeight());

}
}

}

和我在这里的屏幕,处理UI和功能

and here my screen that handle ui and functionality

public class ChatScreen extends MainScreen implements FieldChangeListener, FocusChangeListener {



    class MainTask implements Runnable
    {

        public MainTask()
        {}

        public void run() {

            try
            {
                if(textMsg.getText().equals(""))
                {   
                    IssueHTTPPostL();
                }
                else
                {
                    IssueHTTPPostChat(textMsg.getText());
                }
                UiApplication.getUiApplication().invokeLater(new Runnable() {

                    public void run() {
                        if(replyCode.equalsIgnoreCase("success"))
                        {   




                            if(chatMsgData!=null && chatMsgData.length>0 )
                            {

                                for( int i=0; i<chatMsgData.length; i++)
                                {


                                    final String name = chatMsgData[i].name;
                                    final String email = chatMsgData[i].email;
                                    final String date = chatMsgData[i].date;
                                    final String msg = chatMsgData[i].message;
                                    final String vappUser = chatMsgData[i].vappUser;

                                    if(chatMsgData[i].isReply.equals("0"))
                                    {   

                                        vfm = new VerticalFieldManager(VerticalFieldManager.FIELD_RIGHT)
                                        {
                                            protected void paintBackground(Graphics g) 
                                            {

                                                g.setColor(Color.WHITE);
                                                g.fillRoundRect(30, 0, getWidth()-30, getHeight(), 12, 12);
                                                g.setColor(0X626262);
                                                g.drawRoundRect(30, 0, getWidth()-30, getHeight(), 12, 12);
                                                //g.drawBitmap(0,0,getWidth(),getHeight(),imgRight,0,0);
                                            }   

                                        };

                                        lblName = new LabelField(name, LabelField.FIELD_LEFT|DrawStyle.LEFT)
                                        {
                                            protected void paint(Graphics g) 
                                            {
                                                g.setColor(Color.BLACK);
                                                super.paint(g);
                                            };
                                        };

                                        if(name.equals("")||name.equals("Name"))
                                        {
                                            lblName.setText(email);
                                        }

                                        lblDate = new LabelField(date, LabelField.FIELD_RIGHT|DrawStyle.RIGHT);
                                        lblDate.setFont(font.derive(Font.PLAIN, dateFontSize));
                                        lblName.setFont(font.derive(Font.BOLD, nameFontSize));
                                        JustifiedHorizontalFieldManager hfm = new JustifiedHorizontalFieldManager(lblName, lblDate, true);


                                        hfm.setMargin(10, 10, 10, 50);

                                            lblMsg = new EditField(EditField.FOCUSABLE|EditField.READONLY)
                                            {
                                                protected void paint(Graphics g) 
                                                {
                                                    g.setColor(0x626262);
                                                    super.paint(g);
                                                };
                                            };


                                        lblMsg.setText(msg);
                                        lblMsg.setMargin(10, 10, 10, 50);
                                        lblMsg.setFont(font.derive(Font.PLAIN, msgFontSize));
                                        vfm.add(hfm);

                                        vfm.add(lblMsg);
                                        vfm.setMargin(10, 20, 10, 0);
                                    }
                                    else
                                    {

                                        vfm = new VerticalFieldManager(VerticalFieldManager.FIELD_LEFT)
                                        {
                                            protected void paintBackground(Graphics g) 
                                            {
                                                g.setColor(Color.WHITE);
                                                g.fillRoundRect(0, 0, getWidth()-30, getHeight(), 12, 12);
                                                g.setColor(Color.BLACK);
                                                g.drawRoundRect(0, 0, getWidth()-30, getHeight(), 12, 12);
                                                //g.drawBitmap(0,0,getWidth(),getHeight(),imgLeft,0,0);
                                            }
                                        };


                                        lblName = new LabelField(vappUser, LabelField.FIELD_LEFT|DrawStyle.LEFT)
                                        {
                                            protected void paint(Graphics g) 
                                            {
                                                g.setColor(Color.BLACK);
                                                super.paint(g);
                                            };
                                        };

                                        if(name.equals("")||name.equals("Name"))
                                        {
                                            lblName.setText(email);
                                        }

                                        lblDate = new LabelField(date, LabelField.FIELD_RIGHT|DrawStyle.RIGHT)
                                        {
                                            protected void paint(Graphics g) 
                                            {
                                                g.setColor(0X626262);
                                                super.paint(g);
                                            };
                                        };
                                        lblDate.setFont(font.derive(Font.PLAIN, dateFontSize));
                                        lblName.setFont(font.derive(Font.BOLD, nameFontSize));
                                        JustifiedHorizontalFieldManager hfm = new JustifiedHorizontalFieldManager(lblName, lblDate, true);

                                        //hfm.setMargin("top", "right", "bottom", "left");
                                        hfm.setMargin(10, 50, 10, 10);


                                            lblMsg = new EditField(EditField.FOCUSABLE|EditField.READONLY)
                                            {
                                                protected void paint(Graphics g) 
                                                {
                                                    g.setColor(0x626262);
                                                    super.paint(g);
                                                };
                                            };


                                        lblMsg.setText(msg);
                                        lblMsg.setMargin(10, 40, 10, 10);
                                        lblMsg.setFont(font.derive(Font.PLAIN, msgFontSize));
                                        vfm.add(hfm);

                                        vfm.add(lblMsg);

                                        vfm.setMargin(10, 10, 20, 0);



                                    }


                                    vsm.add(vfm);


                                }   

                            }
                            add(vsm);

                            add(sendHfm);
                        }

                        else
                        {

                            timer.cancel();
                            UiApplication.getUiApplication().popScreen(ChatScreen.this);
                            UiApplication.getUiApplication().pushScreen(new ChatListScreen());
                        }
                    }
                });

            }
            catch(Exception e)
            {e.printStackTrace();}
        }

    }









}

推荐答案

如果我理解正确的话,你有一个 VerticalFieldManager 在屏幕的中间(顶部下方酒吧,以上发送按钮)。你想拥有其内容的底部,该经理开始。对吧?

If I understand you correctly, you have one VerticalFieldManager in the middle of the screen (below the Top Bar, and above the Send button). You would like to have that manager start at the bottom of its content. Right?

对于这一点,尝试使用

vsm.setVerticalScroll(vsm.getVirtualHeight());

其中, VSM 是垂直领域的经理包含旧只读消息。

where vsm is that vertical field manager that contains the old read-only messages.

每当有事情发生(如页面刷新)可能采取集中关闭 BasicEditField ,你可以得到它回来

Anytime something happens (like a page refresh) that might take focus off your BasicEditField, you can get it back with

textMsg.setFocus();

其中, textMsg 是编辑字段。很明显,为了这个工作,你需要保持 VSM textMsg 成员变量,而不是仅仅创造他们作为本地变量,并将它们添加到您的屏幕添加()

where textMsg is the edit field. Obviously, in order for this to work, you need to keep vsm and textMsg as member variables, instead of just creating them as local variables and adding them to your screen with add().

这篇关于如何创建自定义滚动总是从底层做起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆