I had a similar problem recently. The fill valve (a Watermaster brand) was intermittently leaking, and for some reason (I didn't bother to autopsy the old valve, which I installed back in 1996, and which performed faithfully for nearly 15 years - no complaints) this would trigger the fill action; the float (or something internal) would stop the flow and then it would start again. Since this particular model of the valve shut off with a snap, there was a definite water hammer, at a frequency of about 2 cycles per second. This osillation could be started by a sudden pressure drop in the system (such as the washing machine fill valve opening) or it could self-start (usually about 3AM).
Needless to say, this was not likely to improve the plumbing system. I could generally stop it for a while (days) by jiggering the float valve arm, but a replacement valve solved the problem completely.
If there is a lesson from all this, it is that, in most houses, the toilet fill valve is the only automatic system in the plumbing. Everything else - washing machines, dishwashers, various faucets, etc. tends to operate only upon command. The toilet fill valve will operate whenever the water level in the tank drops or (in my case) from an internal malfunction. Way back when, I worked as a plumber for a few years, and the most common problem was toilet repair, so I learned to hate fixing toilets!
In all the houses and remodels I have done, I've always required my plumber to install water hammer arrestors like the ones described by Sliderulefarmer in the hot/cold lines adjacent to every electrically operated valve; washing machine, dish washer. Water hammer probably destroys more water mains and causes more damage inside houses than most other single events. I've seen 30 ton fire trucks actually moved sideways when a valve was incorrectly slammed shut on a 5" line. Good observation, connecting the toilet valve and knocking noise.
While you did stop the hammering by finding the source of initially oscilating valve, the true problem lies in poorly installed lines. Having worked under a great plumbers watchful eye, he always made sure we secured the lines throughout the walls. Water hammering is started by a valve opening and closing quickly, toilet, washer, dish washer, even shower valves can do this at times, but the real noise comes from the pipes banging on the studs and walls somewhere in the house. To really stop this from ever happening again you need to install a water hammer arrester. Sounds complex but it is simply a short piece of capped pipe (<1 foot) installed vertically somewhere in the system, being vertical it holds a small air pocket at the top that dampens & eliminates water hammering. You need to install one on both the hot and cold lines. With the new PEX piping this has become less of a problem as PEX seems to have a little give in it that dampens the hammering.
Focus on Fundamentals consists of 45-minute on-line classes that cover a host of technologies. You learn without leaving the comfort of your desk. All classes are taught by subject-matter experts and all are archived. So if you can't attend live, attend at your convenience.