Unmanned aerial vehicles (UAVs) are capable of serving as a data collector for wireless sensor networks (WSNs). In this paper, we investigate an energy-effective data gathering approach in UAV-aided WSNs, where each sensor node (SN) dynamically chooses the transmission modes, i.e., (1) waiting, (2) conventional sink node transmission, (3) uploading to UAV, to transmit sensory data within a given time. By jointly considering the SN's transmission policy and UAV trajectory optimization, we aim to minimize the transmission energy consumption of the SNs and ensure all sensory data completed collected within the given time. We take a two-step iterative approach and decouple the SN's transmission design and UAV trajectory optimization process. First, we design the optimal SNs transmission mode policy with preplanned UAV trajectory. A dynamic programming (DP) algorithm is proposed to obtain the optimal transmission policy. Then, with the fixed transmission policy, we optimize the UAV's trajectory from the preplanned trace with recursive random search (RRS) algorithm. Numerical results show that the proposed scheme achieves significant energy savings gain over the benchmark schemes.