BART trains have these spots for bikes, with adjustable straps to hold bikes upright. But with a strap or two secured the bike still rolls back and forth as the train moves, which lets it knock into things.
The way to prevent rolling is to hold down a brake lever. You can do that with a large velcro strap, or your hand. But if you don’t have a strap, and you want to sit down, you can wrap your helmet straps the handle, and connect the buckle. If needed you can twist the straps to make them shorter. You don’t have to press the lever all the way down, just enough to stop the wheel from rolling.

You’ve reminded me that I need to do something similar but for my e-scooter. When folded up, it will try to roll away as the bus yaws or pitches. But my plan is to drill a hole through the bottom of brake lever housing, so I can push a bolt up that will jam the brake lever whilst engaged. And since the bolt is facing downward, it should fall out when I squeeze the brake, quickly putting the scooter back into action.