Is this a bug (widget.scrollView + widget.newSegmentedControl) ? It's not working!

Consider following code. It’s very simple: create a scrollview & put a segmented control on it:

 local widget=require("widget") local function onSegmentSwitch(event) local target = event.target print( "Segment Label is:", target.segmentLabel ) print( "Segment Number is:", target.segmentNumber ) end mScrollView = widget.newScrollView { top = 0, left = 0, width = display.contentWidth, height = display.contentHeight , hideBackground = true, --listener = scrollListener, --hideScrollBar = true, } local segmentedControl = widget.newSegmentedControl { top = 100, left = 100, segments = { "1", "2", "3", "4", "5"}, segmentWidth = 40, defaultSegment = 3, onPress = onSegmentSwitch, } mScrollView:insert(segmentedControl)

And from my testing, it’s not working! When I click on the segmented control buttons, nothing happens.

If I comment out the last line,

-- mScrollView:insert(segmentedControl)

It works then. (Of course)

Am I doing anything wrong here? 

P.S. I am using daily build 2108.

Tried this on my test app and can confirm same behavior. Seems like scrollView is not allowing newSegmentedControl take focus. It looks like a bug. 

Thanks for the testing & confirmation, ksan.

I will file a bug report then.

Bug report case 29097.

This case (scrollView + segmented control) is quite common & fundamental. For example, a setting page. Hope Corona can fix it as soon as possible.

Great catch. Will enter it in my list as well if you don’t mind.

Yup, got bitten by that one too (outside of a scrollView). Fixed it for my use case scenario but didn’t check it in github as it needs a much wider testing.

Joe, if you are into patching widgets and in a hurry, I will send you the quick fix to get you going

Thanks

Atanas

Hi, Atanas,

I am not really needing it in a hurry, but do wish a fix within 2 weeks from Corona.

And I am also interested in how you could fix this. I would like to learn your quick fix if you don’t mind sharing it.

Thanks.

Joe

Hi Joe,

I am just eliminating all parents with a negative offset 

if you look at view:touch(event), those are the lines I changed (added a math.max) :

 

local currentSegmentLeftEdge = segmentedControlXPosition + ( segmentWidth * currentSegment ) - segmentWidth + math.max(0,parentOffsetX)

local currentSegmentRightEdge = segmentedControlXPosition + ( segmentWidth * currentSegment ) + math.max(0,parentOffsetX);    

Works for your scrollView case and for my case with a custom pageSlider widget. Wont work with displayGroups with negative offsets.

Atanas

@atanas

Sorry I don’t really understand how to use your code

You calculated two variables and then how to use them?

And how about other parameters?

I guess I missed some steps :slight_smile:

in widget_segmentedControl.lua, around line 291 in function view:touch( event )

replace the lines 

local currentSegmentLeftEdge = segmentedControlXPosition + ( segmentWidth * currentSegment ) - segmentWidth + parentOffsetX

local currentSegmentRightEdge = segmentedControlXPosition + ( segmentWidth * currentSegment ) + parentOffsetX

 

with 

 

–add the math.max to take into account only positive parent offsets

 

local currentSegmentLeftEdge = segmentedControlXPosition + ( segmentWidth * currentSegment ) - segmentWidth + math.max(0,parentOffsetX)

local currentSegmentRightEdge = segmentedControlXPosition + ( segmentWidth * currentSegment ) + math.max(0,parentOffsetX);

 

Cheers,

  Atanas

I didn’t know I could change the source code for widgets.

I will see if I could find how to do it.

Thanks!

Start here : http://forums.coronalabs.com/topic/34183-widgets-20-open-source/

You need to download the code from GitHub, copy it to your project directory and override the built-in version that comes with your installed Corona SDK.

Is this the one I should be looking for?

https://github.com/coronalabs/framework-widget/commits/master

but the last update was Nov, 13, 2013… It seems not the latest build?

I really hope Corona can fix this bug instead. 

My setting page totally needs this fundamental fix.

Hi joe, i am pretty sure Alex is fixing this issue in the next update. If not, I uploaded a more robust fix at https://github.com/atanasster/framework-widget/blob/master/widgetLibrary/widget_segmentedControl.luaThis fix now uses contentToLocal to calculate the coordinates

@atanas

Thanks a lot for the update.

Are you branching out a new version of the widget?

Hi Joe,

Well - its a branch, but I hope it gets eventually merged when Corona pushes a new version of the source code as I don’t really have the time to maintain a full branch of the widgets.

Additionally I am adding some new “widgets” and extending some of the current ones - right now I have uploaded a newEditField widget and a mod to tableView to allow for a sliding row where we can put delete buttons or other widgets. Have a couple more that I needed for my apps that I will upload eventually. 

Thanks,

  Atanas

Also, I am not really overwriting the whole widgets library to install a patch as in the post you were redirected to.

What you can do is -

  1. Download the file you need (for example widget_segmentedControl) for my branch and copy it into a widgets folder of your project

  2. Download my widgetext.lua and also place it in the widgets folder.

  3. Edit widgetext.lua to contain only the hooks for the files you need to replace from the CL widgets lib 

  in your case, its the segmentedControl portion :

local function newSegmentedControl( options )

    local theme = _getTheme( “segmentedControl”, options )

    local _segmentedControl = require( “widgets.widget_segmentedControl” )

    return _segmentedControl.new( options, theme )    

end

widget.newSegmentedControl = newSegmentedControl;

  1. in your main.lua file add a require - 

require(“widgets.widgetext”) 

This will make you use only a custom version of segmentedControl and all the other files from the default CL widgets lib

Atanas

Atanas’s version rocks! If you’re in a rush go get it and you’ll be all set! I confirmed that it works in your use case. 

On a side note, I really like Atanas’s way of substituting modified widgets selectively through widgetext.lua. Previously, we had to do all or none using the GitHub version of the widget code. This approach is very flexible. 

Last but not least, I see the cat is out of the bag on “Slide the row to get IOS style Delete button…” Just have to say that thing is amazing. Works just like IOS7. 

Great work Atanas. Thank you so much for all your contribution. 

Thanks a lot. I am using this method to fix the segmented control problem for now.

By the way, I have been seeing “changing order” feature by dragging an item up or down in some apps. Do we have this kind of feature somewhere in the widget as well?

Tried this on my test app and can confirm same behavior. Seems like scrollView is not allowing newSegmentedControl take focus. It looks like a bug.