Components inside List in CodenameOne

£可爱£侵袭症+ 提交于 2019-12-11 07:49:54

问题


my question is. Is possible to add a component like a button (button has a functionality that triggered when it is clicked) inside a list component?

This image explain better what I refer:

http://2.bp.blogspot.com/-HThpKcgDyRA/URI_FdpffMI/AAAAAAAAAUI/SficZAPXaCw/s1600/1.png


回答1:


Yes but it requires some handcoding and it will only work for touch (since you won't be able to assign focus to it).

We normally recommend just using Component/Container hierarchies for these cases rather than dealing with lists but obviously this isn't always practical.

The key is to always use the list action listener to trigger events, nothing else. So when you are in the action handling code of the list you would want to know if it was triggered by your button...

If you are in the GUI builder this is pretty easy:

Button b = ((GenericListCellRenderer)list.getRenderer()).extractLastClickedComponent();
if(b != null && b == myButton) {
   // your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}

The handcoded approach is pretty similar with one major caveat, you don't have the extractLastClickedComponent method. So assuming you have a component within the renderer just add an action listener to it. Within the action listener just set a flag e.g.:

myButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ev) {
       buttonWasClicked = true;
    }
});

// within the list listener we do the exact same thing:
if(buttonWasClicked) {
   // for next time...
   buttonWasClicked = false;

   // your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}



回答2:


Yes!!! Try it, this is pretty easy:....

http://www.codenameone.com/how-do-i---create-a-list-of-items-the-easy-way.html

http://www.codenameone.com/how-do-i---create-a-list-of-items-the-hard-way-gui-builder-renderer.html



来源:https://stackoverflow.com/questions/19459008/components-inside-list-in-codenameone

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