The ruling is a setback for efforts to claim that Virginia’s legislative districts have been gerrymandered to favor Republicans and disenfranchise black voters, who tend to vote Democratic, but the matter is likely to be appealed to the state Supreme Court.