The reason people advise people to learn melodies is that most melodies land on chord tones on strong beats of the measure. Thus the SOUND that you are learning is very good to get into your inner ear. But to learn the THEORY behind this, take a photocopy of famous song chart that has melody notes and chords symbols, and and right where you would play each chord, take a marker and circle the note in the melody right where a chord is to be played. Then mark down the degree number of that melody note as related to that chord. If the chord is Dm7, and the note is F, write down a 3. Now count up all the chords, all the 1s, 3s, 5s, and 7s, and you will see how that composer used chord tones as melody notes on strong beats.
Start working on arpeggios and start working on intervals. By intervals, I mean moving up and down your scale patterns in intervals of more than a half note or whole note. If you decide to play the C Major scale in fourths, you would play C F D G E A, etc up and then down the same way.
You need to start moving in intervals of thirds and greater, working on landing on chord tones on strong beats, and connecting those strong beats with combinations of chord tones, scale notes, intervals, and chromatic notes. Do not mix it up right away. Decide to play through a chord chart always landing on the third of each chord, using only chord tones. Then add scale tones. Then chord tones one measure and scale tones the next measure. Then do the same landing on the fifth, the seventh, and the root/tonic/one.