Actually, water hammer is not caused by a spike in pressure, although that would be the symptom of the disease.
It's caused by momentum. Water is heavy and when it's moving quickly it has a lot of momentum. Momentum is energy. In fact, when Isaac Newton took pen in hand and wrote his first law of motion, he didn't say Force=mass times acceleration, he said Force = the rate of change in momentum, and we're the ones who changed it to the more popular mass times acceleration.
Energy can neither be created nor destroyed, only converted to another form of energy. So when the water molecules in a pipe are forced to stop, their momentum gets transfered to whatever is stopping them from moving STRAIGHT forward in the same direction as the momentum, and that could be an elbow or tee in the piping, or the valve itself. Even if the pipe is securely fastened to the studs and doesn't have any apparant motion, the energy in the momentum of the water will go into stretching that pipe and bending whatever is holding the pipe from moving ever so little before they elastically retract to their former positions.
Imagine a crowded bus making an emergency stop. The people standing in the aisle will all be forced to the front of the bus if they weren't holding themselves from going forward. If they weren't holding themselves steady, the momentum of all those people would be transferred to the front windshield of the bus, possibly breaking it.
I know how the fill valve in a toilet works, and it's a topic we discussed about six or seven months ago in this forum. But, there's nothing I can think of that would make a fill valve close any faster than it normally does, except a higher water pressure. But, I can't see that difference accounting for water hammer.
The way they fasten down piping in the walls I've had any intimate knowledge of is to notch the studs about 8 to 10 inches above the floor and run the piping in the notches. Then they nail a metal plate over each notch and put up drywall to cover everything. Sometimes the plumbers will stick little pieces of wood between the pipe and the metal plate to wedge the pipe in place and keep it from moving.
I'm afraid that whatever was holding that pipe in place has come loose over the past while and the pipe is free to move in the wall now. And, so the result is that you can hear the pipe shaking in the wall when flow stops abruptly.
What should help some is water hammer arrestors. Basically, they're a piston in a sealed cylinder. The momentum of the water pushes on the piston to compress air in the cylinder. But, if you put in a water hammer arrestor, it's important to install it so that it's in the same linear direction as the momentum of the water. That means, if you have a 40 foot horizontal copper pipe coming from your cold water supply, and a 2 foot vertical pipe going up to your toilet's fill valve, the water hammer arrestor should be installed horizontally at the toilet end of the 40 foot long horizontal pipe, not at the toilet end of the 2 foot vertical pipe. Or at least, it'd be most effective in absorbing the momentum of the water if installed horizontally. Ad vertisements that show them installed vertically are aimed at selling water hammer arrestors. If they told you to crawl into the crawl space under your house and install them there, you wouldn't buy them.