I've also become fairly impolite (or, at least, unsociable) in certain public situations. Maybe it's that I'm older now and have more to lose, but I don't see any real value in having conversations with folks rooting around in dumpsters. I agree that it sounds selfish, but I don't know what I'd hope to gain from an exchange like that. How could our interaction possibly enrich *both* our lives?
Having said that, I can think of many things he'd stand to gain. Most of those come at my expense, in varying degrees starting with a dime and ending with my life. If it's a situation where someone needs a hand, I'm glad to help. But in cases like yours, you have to ask yourself "Why has he initiated this interaction?"
Someone at loose ends like him needs to maximize his gain and minimize his loss. In other words, he doesn't have the luxury of striking up a new friendship with a complete stranger that you and I enjoy. Therefore, his intent is to gain something in some way. My prediction is that 99 times out of 100, that gain comes at your loss.
When in a similar situation, ask yourself what it is that you are prepared to lose, and then deal with the situation accordingly. If it's a couple bucks for a guy walking down a hot street holding a gas can, stop and give him a buck or two if you like. If it's a fellow who wants "a couple bucks to get food", same deal. But be aware that in both cases, you run a risk of losing more than you might want to, and so that's why you have to stay prepared and make the decision to engage well beforehand if possible.
Lately, my unsociability has taken the form of actively discouraging the type of encounter that you had. I feel that in the long run I'll be better off for it, and I no longer care to meet new people who have less to lose than I do. I've met interesting people and been in interesting/fun situations in past random public encounters, but I have no desire for that sort of thing any more. Which means, following the above, that I stand to not gain because I don't wish to meet new people and get involved in new situations. But that's an OK choice for me, because what I could lose is much, much more than anything I could possibly have gained.